> For the complete documentation index, see [llms.txt](https://tester-bszelag.gitbook.io/software-testing/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tester-bszelag.gitbook.io/software-testing/selenium-webdriver.md).

# 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](/files/-MFnc8Y2sg8ajjFt-8UG)

![Xpath in firefox console](/files/-MFncSfP4Mvr-2gQf4WL)

```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](/files/-MFnebzi0-aH9pGB6waP)

![In Console](/files/-MFnfA1xK2-XQ41BzFt9)

Extension **Ranorex Selocity**

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

![Ranorex Selocity how to use ](/files/-MFnlx1ik_UVWRLlW7NW)

### 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%

![](/files/-MFB9vFGJifqAbDpg5To)

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

![](/files/-MFBA5gAvE4q6uYGwH1f)

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

![](/files/-MFyZlUz51fr3XrwHFP_)

```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](/files/-MFzCXBzNHsUWIDN67EL)

### parent and sibling

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

![](/files/-MFzOGjumTkH13gVVib5)

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

![](/files/-MFzOwP7Cc1tv19yf1Cz)

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

![](/files/-MFzP3to2ggkKdqn-F4U)

## Practice

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

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

![](/files/-MFzTdAXBDimec8Z8Oat)

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

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

![](/files/-MG-C2h9z7SpiZF4ObTs)

![](/files/-MG-Btgh5_xOS12-ym1s)

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tester-bszelag.gitbook.io/software-testing/selenium-webdriver.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
