# 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

{% embed url="<https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/firefox.html>" %}
page information's&#x20;
{% endembed %}

{% embed url="<https://github.com/mozilla/geckodriver/releases/>" %}
geckodriver
{% endembed %}

```python
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

{% embed url="<https://letskodeit.teachable.com/p/practice>" %}
website to practice
{% endembed %}

![Add-ons Try](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFnbuN5haDCeo9Tdq3A%2F-MFnc8Y2sg8ajjFt-8UG%2Fimage.png?alt=media\&token=7c940f23-1e9b-4df1-8b76-868bf26cbf22)

![Xpath in firefox console](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFnbuN5haDCeo9Tdq3A%2F-MFncSfP4Mvr-2gQf4WL%2Fimage.png?alt=media\&token=875ac92f-7d9b-4763-aaff-7d366bbcb7d0)

```python
#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

{% embed url="<https://chromedriver.chromium.org/>" %}

```python
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](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFnco56Q62AI5qDKGgN%2F-MFnebzi0-aH9pGB6waP%2Fimage.png?alt=media\&token=1bae2fdf-74e4-4d50-a559-cbc795464750)

![In Console](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFnejHVLsEb8naf1Uyh%2F-MFnfA1xK2-XQ41BzFt9%2Fimage.png?alt=media\&token=2c3c0e76-f0ad-4e05-b724-57be681b6336)

Extension **Ranorex Selocity**

1. Inspect element
2. Ranorex Selocity show \<input> css / xpath to copy to automation tests

![Ranorex Selocity how to use ](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFnlcl7Uy0ffWshAzm8%2F-MFnlx1ik_UVWRLlW7NW%2Fimage.png?alt=media\&token=fdb6a4e3-5373-4244-bf4f-ab8ee9306858)

### Internet Explorer

Internet Explorer driver is called IEDriverServer.exe

{% embed url="<https://www.selenium.dev/downloads/>" %}

#### Requirements to run tests on Internet Explorer

1. Set a zoom on 100%

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFB8d1HJQ454Rh9FvrQ%2F-MFB9vFGJifqAbDpg5To%2Fimage.png?alt=media\&token=d73d43c2-038c-4535-b6df-b5e0768c5b04)

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

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFB8d1HJQ454Rh9FvrQ%2F-MFBA5gAvE4q6uYGwH1f%2Fimage.png?alt=media\&token=e9a1d397-67a1-474f-8f94-aefa874fc4b6)

```python
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

```python
from selenium import webdriver #use a selenium package
```

### set a webdriver

```python
#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

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

## Finding elements

### By

```python
#to use 'By' need to import lib
from selenium.webdriver.common.by import By
```

```python
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"
```

```python
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

```css
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']**

```css
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

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFyYCHVJIOW6iEYJXG8%2F-MFyZlUz51fr3XrwHFP_%2Fimage.png?alt=media\&token=3ba990d9-5516-49f9-b817-0d82d4d267b9)

```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']**&#x20;

```css
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’)]**

```css
//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’)]**

```css
//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](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzCLDm4FjSlFFoffcy%2F-MFzCXBzNHsUWIDN67EL%2Fimage.png?alt=media\&token=1cdd731f-6565-4fb1-ae92-0515415e1270)

### parent and sibling

**Parent** \
**Syntax**: xpath-­‐to-­‐some-­‐element//parent::&#x20;

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzO0l09hhIYvmqj3sK%2F-MFzOGjumTkH13gVVib5%2Fimage.png?alt=media\&token=268f316a-c0ed-4fb8-b29d-49d19d7924d0)

**Preceding Sibling** \
**Syntax**: xpath-­‐to-­‐some-­‐element//preceding­‐sibling::&#x20;

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzO0l09hhIYvmqj3sK%2F-MFzOwP7Cc1tv19yf1Cz%2Fimage.png?alt=media\&token=66717bcb-5186-41b8-bf45-df89641b9f4a)

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

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzO0l09hhIYvmqj3sK%2F-MFzP3to2ggkKdqn-F4U%2Fimage.png?alt=media\&token=a7742d92-c9c8-4c63-a040-3b9e2be9faeb)

## Practice

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

[http://letskodeit.teachable.com/p/practice](https://letskodeit.teachable.com/p/practice)

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzPBRl3XbgBP0JUd9Z%2F-MFzTdAXBDimec8Z8Oat%2Fimage.png?alt=media\&token=cc76999b-91f1-4bf9-974a-a349f994e5b8)

#### 2. Find the Project name from Owner Maria Kennedy

<https://dhtmlx.com/docs/products/dhtmlxGrid/><br>

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzUQpPsjlXenyaiD19%2F-MG-C2h9z7SpiZF4ObTs%2Fimage.png?alt=media\&token=e624e65a-1443-4fdd-9d3f-1be9a9532b73)

![](https://4075407169-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCCXrRYTXdf78GMukgv%2F-MFzUQpPsjlXenyaiD19%2F-MG-Btgh5_xOS12-ym1s%2Fimage.png?alt=media\&token=c2e0b8d0-6e20-4532-9e3f-cc3fd84fc8d9)

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