Simple example in Selenium Python
Published in
2 min readFeb 26, 2022
Lets see a sample program and how selenium python works.
Sample Program:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time # initialising webdriver
PATH = "/path/to/chromedriver"
driver = webdriver.Chrome(PATH) def startpy():
driver.get("https://www.google.com/")
# gets first element from the given tag
search = driver.find_element_by_name("q")
search.send_keys("cats")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()if __name__ == '__main__':
startpy()
This program basically automates a simple google search.
First you need to import selenium webdriver for using any commands. The Keys module is imported for using the send_keys() function.
After downloading your corresponding driver — in this case chromedriver — you need to start the selenium webdriver.
# initialising webdriver
PATH = "/path/to/chromedriver"
driver = webdriver.Chrome(PATH)
The get() function is used to open the webpage. In this example we use google.
driver.get("https://www.google.com/")
The find_element_by_name() command is used to find an element by its name.
The send_keys() function types content and hits Keys. In this example we use it to type “cats” and hit the enter key.
search.send_keys("cats")
search.send_keys(Keys.RETURN)
The output: