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
Post a Comment