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

page information's
geckodriver
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

website to practice
Add-ons Try
Xpath in firefox console
#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

Find elements by use cltr+F
In Console

Extension Ranorex Selocity

  1. Inspect element

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

Ranorex Selocity how to use

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']
1 matching node

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

Was this helpful?