Selenium
Intro | |
Use WebDriver in Windows | |
Use WebDriver in Linux | |
Make Selenium Browser Fullscreen | |
Find proper selector | |
Errors |
Intro
Selenium WebDriver — it is a tool for automating web browser actions.
In most cases, it is used for testing Web applications, but it is not limited to this.
Selenium Webdriver itself, unlike, for example, the Selenium IDE, does not contain anything for testing,
so it is used in conjunction with testing libraries that are available in most modern
programming languages.
In
Python
there are unittest, nose, pytest and others. In
Java
it is e.g. JUnit.
In particular, it can be used to solve routine site administration tasks or
regularly receiving data from various sources (sites).
In this article, you will learn how to use Selenium without being tied to any programming language.
«Selenium + Python
article contains guides on writing tests with
Python
and Selenium
.
To read about
Java
and Selenium you can visit
Selenium + Java
page
Add WebDriver in Windows
Visit
selenium.dev
and download the driver for the desired browser.
For example for
Firefox
Download the archive with the driver. For Windows x64, select .zip file
Unpack the archive. I unpacked it into a folder C:\webdrivers
Add the path to this folder to the system variable
PATH
.
In the search, type env
In System Variables click New
Create a new variable named WEBDRIVER_PATH in the value field specify the path to the folder in which you have to put geckodriver.exe
Select the Path variable and click Edit
Click New and enter %WEBDRIVER_PATH%
Add WebDriver to Linux
First: you need to download WebDriver with exactly the same version as your actuall Chrome has.
Same applies to Firefox
Second: you need to add directory with WebDriver to system
PATH
Check Chrome version
Check your google-chrome version by running
google-chrome --version
Google Chrome 89.0.4389.114
Chrome version can be checked in GUI as well. Open browser and visit Settings → About Chrome
Upgrade google-chrome to latest available stable version for linux
sudo apt-get update
sudo apt-get --only-upgrade install google-chrome-stable
Check the updated version
google-chrome --version
Google Chrome 90.0.4430.93
Visit selenium.dev and download
- chromedriver_linux64.zip for Chrome ( Download )
- or geckodriver-v0.29.1-linux64.tar.gz for Firefox ( Download )
Note that you should download exactly the same version as you have in your system.
Lets proceed assuming that they are now in a Downloads folder.
If they are in another folder please replace /Downloads with the proper path
ls -l ~/Downloads | grep linux64
-rw-r--r-- 1 andrei urnsu 5891445 May 6 15:59 chromedriver_linux64.zip -rw-r--r-- 1 andrei urnsu 2716874 May 6 16:33 geckodriver-v0.29.1-linux64.tar.gz
Create directory /opt/WebDriver/bin and copy the driver archives there
sudo mkdir -p /opt/WebDriver/bin
sudo cp ~/Downloads/chromedriver_linux64.zip ~/Downloads/geckodriver-v0.29.1-linux64.tar.gz /opt/WebDriver/bin
ls -l /opt/WebDriver/bin
-rw-rw-r-- 1 andrei andrei 5564194 Nov 20 10:05 chromedriver_linux64.zip
-rw-rw-r-- 1 andrei andrei 2650003 Nov 20 10:05 geckodriver-v0.29.1-linux64.tar.gz
Go to /opt/WebDriver/bin and uppack the archives
cd /opt/WebDriver/bin
sudo tar -xvzf geckodriver-v0.29.1-linux64.tar.gz
sudo unzip chromedriver_linux64.zip
ls -laFh /opt/WebDriver/bin
total 27M drwxr-xr-x 2 root root 4.0K Nov 20 10:46 ./ drwxr-xr-x 3 root root 4.0K Nov 20 10:07 ../ -rwxr-xr-x 1 root root 11M Oct 15 23:34 chromedriver* -rw-r--r-- 1 root root 5.4M Nov 20 10:19 chromedriver_linux64.zip -rwxr-xr-x 1 sshit sshit 7.6M Nov 3 18:13 geckodriver* -rw-r--r-- 1 root root 2.6M Nov 20 10:19 geckodriver-v0.29.1-linux64.tar.gz
After successful unpacking, you can make sure that there are two executable files:
chromedriver
and
geckodriver
Add /opt/WebDriver/bin to PATH
Open
.bashrc
and add the following code there
vi ~/.bashrc
PATH="/opt/WebDriver/bin:${PATH}"
export PATH
Restart terminal
exec bash
Make Selenium Fullscreen
The first way is to open it in kiosk mode
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--kiosk")
driver = webdriver.Chrome(options=chromeOptions)
The second way is to set the driver a width equal to the width of your screen
driver.set_window_size(1920, 1024)
Actions
clear, click, findElement, findElements, getAttribute, getCssValue, sendKeys, submit, isDisplayed, isEnabled, getLocation, isSelected, getSize, getTagName, getText,
Determine the appropriate selector
If something doesn't work out manually, you can try online services, for example try.jsoup.org
Errors
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
Usually appears if you are trying to execute < b>SendKeys on an element that doesn't support it.
SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 91
Current browser version is 89.0.4389.114 with binary path /usr/bin/google-chrome
Means that you have downloaded WebDriver with a different version than your installed browser has.
These versions should match.
Update your Chrome to latest version (may not match still) or download chromedriver that matches your installed version as
described
here