Selenium WebDriver

Tests on Browsers

Remember to check version of WebDriver with current browser version.

  1. Create a new Python Package in your project

  2. Create a new Python File (and then code and run there)

Mozilla Firefox

Firefox WebDriver is called geckodriver.exe

from selenium import webdriver

class RunFFTests():
    def testMethod(self):
        driver = webdriver.Firefox(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\geckodriver.exe")
        driver.get("http://www.google.com")

ff = RunFFTests()
ff.testMethod()

After this program the Firefox doesn't close.

Xpath

#examples
$x("//input[@id='input']")
$x("//input[contains(@class, 'btn-style')]")
$x("//input[@class='btn-style']")

Google Chrome

Google Chrome driver is call chromedriver.exe

from selenium import webdriver

# class ChromeDriver():
#     def testMethod(self):
#         driver = webdriver.Chrome(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\chromedriver.exe")
#         driver.get("http://www.google.com")
#
# cc = ChromeDriver()
# cc.testMethod()

driver = webdriver.Chrome(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\chromedriver.exe")
driver.get("http://www.google.com")

When using class in ChromeDriver, after the page is load the browser will close. Without class the browser stay on this site.

Xpath

Extension Ranorex Selocity

  1. Inspect element

  2. Ranorex Selocity show <input> css / xpath to copy to automation tests

Internet Explorer

Internet Explorer driver is called IEDriverServer.exe

Requirements to run tests on Internet Explorer

  1. Set a zoom on 100%

2. Make sure that Protected Mode on every of 4 types of zone are the same (Disable or Enable) a) Internet b) Local intranet c) Trusted sites d) Restricted sites

from selenium import webdriver

class IEDriverWindows():
    def testMethod(self):
        driver = webdriver.Ie(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\IEDriverServer.exe")
        driver.get("http://www.google.com")

ieObject = IEDriverWindows()
ieObject.testMethod()

Safari

It's only supported on Macs. (Apple)

Commands

import webdriver

from selenium import webdriver #use a selenium package

set a webdriver

#chrome
driver = webdriver.Chrome(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\chromedriver.exe")
#firefox
driver = webdriver.Firefox(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\geckodriver.exe")
#IE
driver = webdriver.Ie(executable_path="C:\\Users\\nn\\PycharmProjects\\drivers\\IEDriverServer.exe")

open a URL

#e.g. www.google.com
driver.get("http://www.google.com")

Finding elements

By

#to use 'By' need to import lib
from selenium.webdriver.common.by import By
class By(object):
    """
    Set of supported locator strategies.
    """

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"
driver.find_element(By.XPATH,	"xpath	expression")
#e.g.
elementById = driver.find_element(By.ID, "name")
driver.find_element(By.XPATH, "//input[@id='displayed-text']")
elementByClassName = driver.find_element(By.CLASS_NAME, "table-display")

CSS Selectors

Syntax: tag[attribute='value']

"#" -> Id "." -> Class

FROM:
/*
<input id="displayed-text" 
name="show-hide" 
class="inputs displayed-class" 
placeholder="Hide/Show Example" 
type="text">
*/

input[id='displayed-text']
#displayed-­text   /*be careful on the same id in another class*/
input#displayed-­text

input[class='inputs displayed-class'] /*whole name of class should be used*/
.displayed-­class
input.displayed-­class

Appending Classes

.class1.class2.class3 -> Until we find a unique element

Wildcards in CSS Selectors

Syntax: tag[attribute<special character>='value']

FROM:
/*
<input id="name" name="enter-name" 
class="inputs" placeholder="Enter Your Name" 
type="text" style="">
&
<input id="displayed-text" name="show-hide" 
class="inputs displayed-class" 
placeholder="Hide/Show Example" 
type="text" style="">
*/
input[class='inputs'] -> only 1 matching
input[class^='inputs'] -> 2 matching (2 times class starts from inputs)
input[class^='inp'] -> 2 matching the same like in inputs
input[class='displayed-class'] -> no maching (because not whole name of class)
input[class$='class'] -> 1 matching (from displayed-class)
input[class*='displayed-class'] -> 1 matching (second word of class)
input[placeholder='Enter'] -> no maching
input[placeholder^='Enter'] -> 1 matching

Finding Children CSS

fieldset -> 10 matching
fieldset>table -> 1 matching
fieldset>#product -> 1 matching (id='product')

another fieldsets
fieldset>button -> 1 matching
fieldset>table.table-display

Xpath

Single slash ‘/’ anywhere in xpath signifies to look for the element immediately inside the parent element. Double slash ‘//’ signifies to look for any child or nested-­‐ child element inside the parent element.

Syntax: //tag[@attribute='value']

absolute Xpath:
/html/body/header/div/div/div/div/ul/li[2]/a

relative Xpath using single '/':
//div[@id="navbar"]/div/div/div/ul/li[2]/a

effective(optimized)Xpath using double '//':
//div[@id="navbar"]//ul/li[2]/a

contains

Syntax: //tag[contains(attribute, ‘value’)]

//div[@id='navbar']//a[contains(text(),'Login')]
//div[@id='navbar']//a[contains(@class,'navbar-­‐link') and 
contains(@href,'sign_in')]

starts-with

Syntax: //tag[starts-­‐with(attribute, ‘value’)]

//div[@id="navbar"]//a[contains(@class, 'navbar-link')] -> 2 matching
//div[@id="navbar"]//a[starts-with(@class, 'navbar-link')] -> 1 matching
/*
check first of class name which is using */

the most shortest path is:
//a[@href='/sign_in']

parent and sibling

Parent Syntax: xpath-­‐to-­‐some-­‐element//parent::

Preceding Sibling Syntax: xpath-­‐to-­‐some-­‐element//preceding­‐sibling::

Following Sibling Syntax: xpath-­‐to-­‐some-­‐element//following-­sibling::

Practice

1. Find the price of the course "Python Programming Language"

http://letskodeit.teachable.com/p/practice

2. Find the Project name from Owner Maria Kennedy

https://dhtmlx.com/docs/products/dhtmlxGrid/

//div[@id="grid-demo"]//div[(text()='Maria Kennedy')]//parent::div//preceding-sibling::div[2]/div

Last updated