Skip to content

Instantly share code, notes, and snippets.

@junaidtk
Created August 8, 2020 09:32
Show Gist options
  • Save junaidtk/887e84dc6317438e6e4b99b02d1e42ce to your computer and use it in GitHub Desktop.
Save junaidtk/887e84dc6317438e6e4b99b02d1e42ce to your computer and use it in GitHub Desktop.
Web Automation Testing using Selenium and Python
Web Automation Testing using Selenium and Python
=================================================
First you need to check the python is already installed in the system.
In order to check this just type the below command
$ python or $ python3
It will result the as given below
Python 3.7.5
This is showing the installed version of python in the system.
Next we need to check the selienium is available in the python environment.
to check this follow below command.
$ python3
>>> import selenium
>>> help (selenium)
or
$ python3 -c "import selenium; print(selenium.__version__)"
If you are getting the error, then we can say that we need to install the selenium.
Other wise we can understand that, it already added to to the python
Then add webdriver for the automated testing. We can use the diffrent browser webdriver like
driver for chrome, firefox, Edge, safari
you can download the chrome driver from the below URL
https://sites.google.com/a/chromium.org/chromedriver/downloads
Once downloaded, Then follow below commands. The driver class will connect you to browser instant nad key calss emulate the stroke of keyboard.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
Here we are assuming the dirver file is in the same directoy of python file. In this python file we are saving the automation code.
driver = webdriver.Chrome('./chromedriver')
if you getting the “chromedriver” cannot be opened error, then just right click the driver file and then open. It will solve the issue.
Then we can use the get() method of driver.
driver.get("https://www.testsite.org")
This will load the testsite website
if you want to print the title of the website, then you can run below command.
print(driver.title)
This will output
testsite website title
You can run multiple command on this session.
To close the current session, then you can use below command.
driver.close()
Also you can run the python file by adding all above code in the python file as follows.
add all command in the python file and save in the file name selenium_test.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Then save the same directoty of python file and execute the python file with below command.
$ python3 selenium_test.py
This execute all above code in a single strech.
You can refer below url for the detailed explanation.
https://www.browserstack.com/guide/python-selenium-to-run-web-automation-test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment