Saturday, January 13, 2018

How to use Selenium

At first, install python3 from here: Installer Python3 dans Windows

Start your virtual machine (see here if you don't know how to start the virtual machine):
https://surlaprogrammation.blogspot.jp/2017/08/virtual-box-et-vagrant.html

And go to the virtual machine's website that you want to test (usually it's here if you are using vagrant): http://192.168.33.10/

Install Chromedriver (for your host machine):
https://sites.google.com/a/chromium.org/chromedriver/downloads

And change the location of the driver to your workspace, like "C:\Users\John\Documents\workspace\test" for example.


Do the following command on cmd.
pip install selenium

Write as follows in a text file and change it to a python file (.txt -> .py):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

driver_path = "C:\\Users\\John\\Documents\\workspace\\test\\chromedriver.exe" #Changez ici

driver = webdriver.Chrome(driver_path)
driver.get("http://192.168.33.10")
title = driver.find_element_by_tag_name("title")
driver.quit()
#driver.close()

Do the following commands and start the python program which we created just now.
cd C:\Users\John\Documents\workspace\test
python test.py

It will automatically start Chrome and go to http://192.168.33.10 and check the existence of "title" element in the page. (If it doesn't find the tag, it will throw an exception and stops the execution).