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' | ...

Selenium - Basics

Selenium: Selenium is an open source automated testing Suite for web applications across different browsers and platforms. Selenium has 4 major components: Selenium Integrated Development Environment(IDE) Selenium Remote Control(RC) Web Driver Selenium Grid 1. Selenium Integrated Development Environment: Selenium IDE is the simplest framework in the Selenium Suite and very easiest to learn. It has got record playback and saving tests and inbuilt reporting tool.  2. Selenium Remote Control: Selenium RC was the flagship testing framework of the whole Selenium project for a long time. This is the first automated web testing tool where it allows users to use their programming language they prefer. RC comes in 2 forms: 2.1 Selenium Server:      Selenium Server receives selenium commands from your program, interprets them and reports  back the results of those running tests. Selenium Core is a Java Script program which execute...