Selenium is an open-source, web-based automation tool. Python APIs empower us to connect with a browser through Selenium Web driver. Selenium can send the standard Python commands to different browsers such as Chrome, Firefox, IE on different operating systems to perform different tasks on the browser.
1. Open Terminal/Command Prompt and type:~ pip install selenium
2. Download the required Web drivers:
Chrome: Click here.
Edge: Click here.
Firefox: Click Here.
Safari: Click Here.
3. Install the drivers on Windows
Download the web-driver zip file from the above link according to the version of the browser.
Extract the zip file to desired location. (Here C: Drive)
Right click on executable of web-driver and copy the path.
Paste the path in the (executable_path = ‘path’).
4. Install the drivers on mac os
Download the web-driver zip file from the above links.
Extract the zip to the desired location (ex: /usr/bin/chromedriver)
Copy the path of executable to set as the (executable_path = ‘path’).
5. Install drivers on linux
Download the web-driver .
Move the webdriver to the bin folder using. sudo mv driver_name/usr/local/bin
Set the path:
$ exportPATH = PATH:/usr/local/bin/driver_name.
NOTE: In this case we don’t need to specify the executable_path.
Import selenium.
Import webdriver.
Assign path location to a variable.
To maximize the window use maximize_window() function.
1 2 3 4 5 6 7 8 9 10 11
from selenium import webdriver driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe") #For macOS #executable_path = '/usr/bin/chromedriver/chromedriver.exe’ #For Linux #executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’ driver.maximize_window()
Import selenium and web-driver.
Assign path location to a variable.
Use get()
function and pass URL as the parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13
from selenium import webdriver driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe") #For macOS #executable_path = '/usr/bin/chromedriver/chromedriver.exe’ #For Linux #executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’ driver.maximize_window() driver.get("https://www.youtube.com/")
find_element_by_link_text()
FUNCTION.Import selenium and time.
Open the URL using get()
function.
Store the text of the link using find_element_by_link_text()
.
Use time.sleep(5)
to add delay to open the URL.
Finally click on the element with click()
function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from selenium
import webdriver
import time
driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")
#For macOS
#executable_path = '/usr/bin/chromedriver/chromedriver.exe’
#For Linux
#executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’
driver.maximize_window()
driver.get("https://www.topcoder.com/")
time.sleep(5)
element = driver.find_element_by_link_text('Solutions')
element.Click()
Import selenium and time.
Open the URL using get()
function.
Right click on the link to be opened and click on copy link address.
Store the link in a variable using find_element_by_id(‘link’)
.
Use time.sleep(5)
to add delay to open the URL.
Finally click on the element with click()
function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from selenium
import webdriver
import time
driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")
#For macOS
#executable_path = '/usr/bin/chromedriver/chromedriver.exe’
#For Linux
#executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’
driver.maximize_window()
driver.get("https://www.topcoder.com/")
time.sleep(5)
element = driver.find_element_by_id('https://www.topcoder.com/user-selection')
element.Click()
Right click on the field and click inspect.
Search for name attribute in the inspect element page.
Pass the name attribute in find_element_by_name()
function.
Use send_keys()
data to input required data into the field.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from selenium
import webdriver
import time
driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")
#For macOS
#executable_path = '/usr/bin/chromedriver/chromedriver.exe’
#For Linux
#executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’
driver.get("https://accounts-auth0.topcoder.com/#!/member?retUrl=http://www.topcoder.com/")
time.sleep(10)
element = driver.find_element_by_name("username")
element.click()
element.send_keys("this_is_my_username")
element = driver.find_element_by_name("password")
element.click()
element.send_keys("this_is_my_password")
XPath is a technique in Selenium to navigate through the HTML structure of a page. XPath enables testers to navigate through the XML structure of any document, and this can be used on both HTML and XML documents. To learn more about XPath click here.
1 2 3
login_form = driver.find_element_by_xpath("/html/body/form[1]") login_form = driver.find_element_by_xpath("//form[1]") login_form = driver.find_element_by_xpath("//form[@id='loginForm']"
From selenium import webdriver
From pillow import image.
Open the website using get()
.
Save the screenshot to the desired location with save_screenshot()
.
To view the downloaded image use Image.open(‘path’)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from selenium import webdriver from PIL import Image driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe") #For macOS #executable_path = '/usr/bin/chromedriver/chromedriver.exe’ #For Linux #executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’ url = ‘https: //www.topcoder.com/’ driver.get(url) driver.save_screenshot("image.png") image = Image.open("image.png") image.show()
Open the URL using get()
function.
To move forward in the browser use forward()
function.
To move backward in the browser use back()
function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from selenium
import webdriver
import time
driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")
#For macOS
#executable_path = '/usr/bin/chromedriver/chromedriver.exe’
#For Linux
#executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’
driver.maximize_window()
driver.get("https://www.topcoder.com/")
time.sleep(5)
element = driver.find_element_by_id('https://www.topcoder.com/user-selection')
element.Click()
driver.back()
driver.forward()
Open the browser
Add a delay using time.sleep()
function.
Use driver.close()
to close the browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from selenium
import webdriver
import time
driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")
#For macOS
#executable_path = '/usr/bin/chromedriver/chromedriver.exe’\
#For Linux
#executable_path = '/usr/local/bin/chromedriver/chromedriver.exe’
driver.maximize_window()
driver.get("https://www.topcoder.com/")
time.sleep(15)
driver.close()
Have fun with Selenium and WebDriver!