Selenium Python

Install Package

Install selenium python package using pip.

pip install selenium

Download selenium chrome driver and copy to some location, so that you can refer it later in out program.

Google search using selenium script

We are going to do google search programmatically using selenium. The program will open chrome browser and finds the search input via xpath expression and clicks it first, then enters our search keyword using send_keys() method and performs the action.


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

# Chrome browser settings
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en-US')
chrome_options.add_experimental_option('prefs', {'intl.accept_languages': 'en-US'})

# Path to your selenium chrome driver
browser = webdriver.Chrome('path/to/chromedriver', chrome_options=chrome_options)

browser.implicitly_wait(10)

browser.get("https://google.com")

# xpath for the google search text input
search_input = browser.find_elements_by_xpath('//*[@id="lst-ib"]')

search_keyword = "take screenshot selenium python"

action = ActionChains(browser).move_to_element(search_input[0]).click()\
        .send_keys(search_keyword).perform()