Skip to main content

Selenium Basic Commands.

Selenium Commands, Packages and Exceptions.

Here I have selected few most frequently asked commands in the interview.

Recommended way of Importing webdriver from selenium package.
from selenium import webdriver.

Once webdriver is imported then you are able to access the classes like this
  • webdriver.Firefox
  • webdriver.FirefoxProfile
  • webdriver.Chrome
  • webdriver.ChromeOptions
  • webdriver.Ie
  • webdriver.Opera
  • webdriver.PhantomJS
  • webdriver.Remote
  • webdriver.DesiredCapabilities
  • webdriver.ActionChains
  • webdriver.TouchActions
  • webdriver.Proxy

To launch the application URL:

driver = webdriver.Chrome
driver.get('http://youtube.com')

To get URL of the currently loaded page:
driver.current_url

To close the current window:
driver.close()


Exceptions in Selenium:

Base Exception class: Selenium.common.exceptions
  • selenium.common.exceptions.WebDriverException
  • selenium.common.exceptions.InvalidElementStateException
  • selenium.common.exceptions.NoSuchElementException
  • selenium.common.exceptions.ElementClickInterceptedException(msg=None, screen=None, stacktrace=None)
  • selenium.common.exceptions.ElementNotInteractableException(msg=None, screen=None, stacktrace=None)
  • selenium.common.exceptions.ElementNotSelectableException(msg=None, screen=None, stacktrace=None)
  • exception selenium.common.exceptions.ElementNotVisibleException(msg=None, screen=None, stacktrace=None)
  • exception selenium.common.exceptions.ErrorInResponseException(response, msg)
  • exception selenium.common.exceptions.InvalidArgumentException(msg=None, screen=None, stacktrace=None)
  • selenium.common.exceptions.InvalidElementStateException(msg=None, screen=None, stacktrace=None)
  • exception selenium.common.exceptions.InvalidSessionIdException(msg=None, screen=None, stacktrace=None)
  • exception selenium.common.exceptions.NoSuchAttributeException(msg=None, screen=None, stacktrace=None)
  • exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)

Action Chains:

Action Chains are a way to automate low level interactions such as Mouse movements/button actions, key press and doing more complex operations like hover(mouse over), drap and drop operations.

Generate user actions:
When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChain object. When you call perform(), the events are fired in the order they are queued up.


Alerts:

Alerts helps you to interact with alert prompts. It contains methods such as accepting,  dismissing , inputting and getting text from alert prompts.


from selenium.webdriver.common.alert from Alert

Operations
  • Alert(driver).accept()
  • Alert(driver).dismiss()
  • Alert(driver).send_keys(KeysToSend)

Locate elements BY:

  • ClassName
  • CSS_Selector
  • ID
  • Link_Text
  • Name
  • Partial_Link_Text
  • Tag_Name
  • Xpath

Desired Capabilities:

Desired capabilities are essential when we are running concurrent selenium tests on multiple platforms/browsers using selenium grid to achieved performance and browser compatibility.


Option 1 : 

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
selenium_grid_url = "http://198.0.0.1:4444/wd/hub"

#Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy() //To get Global class Instance without altering.
capabilities['platform'] = 'WINDOWS
capabilities['version'] = "10"

#Instantiate an instance of Remote WebDriver with the desired capabilities
driver = webdriver.Remote(desired_capabilities = capabilities, command_executor=selenium_grid_url)


Option 2 : 

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)

driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
   desired_capabilities=DesiredCapabilities.OPERA)

driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
   desired_capabilities=DesiredCapabilities.HTMLUNITWITHJS)


Option 3 : 

The desired capabilities is a dictionary, so instead of using the default dictionaries, you can specify the values explicitly:
driver = webdriver.Remote(
   command_executor='http://127.0.0.1:4444/wd/hub',
   desired_capabilities={'browserName': 'htmlunit','version': '2','javascriptEnabled': True})


Thank You
Happy Learning!

Comments

Popular posts from this blog

Kubernetes ETCDCTL

ETCD is a key-value data store for storing kubernetes state and objects. ETCDCTL is the tool used to back up with snapshot. ETCDCTL a command line tool for interacting with the etcd server  Key Features:  Keep the event history until compaction. access to old version of keys. user controlled key compaction. Support range query. pagination support with limit argument. support consistency guarantee across multiple queries. Replace TTL key with lease. Support watch feature. ETCDTOOL Installation: Build binary from source code  Download tool directly  Build binary from source code: Checkout the code repository git clone -b v3.4.16 https://github.com/etcd-io/etcd.git Change directory to etcd cd etcd Run build script ./build The binaries are under the bin directory. exportPATH="$PATH:`pwd`/bin" Check etcd version etcd --version Download ETCD tool directly: kubectl exec -it etcd-docker-desktop -n kube-system -- /bin/bash -c 'ETCDCTL_API=3 /usr/local/bin/etcd --version' | h

Selenium - Architecture

Selenium Architecture: Here, we will learn about the overall selenium architecture. How communication happens from client library to browser and which are all the components involved between them. Architecture compromises of major four components namely Selenium Language Binding JSON Wire Protocol Browser Drivers Real Browsers 1. Selenium Language Binding: Various programming languages provide their own Rest API support for communicating to their respective browser drivers via JSON Wire protocol. python provides 'selenium ' as a client library which has all the rest API i mplementation  for communicating with browser drivers. Selenium is a third party library which does not come in python basic installation.  You need to install it via PIP command : pip install selenium driver = selenium.webdriver.Chrome('location of the chrome driver executable')  Above line returns one chrome browser session , where rest all browser relate