How to use a click() method in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

While working on an application and navigating to different pages or different sections of a page, we need to click on various UI elements on a page like a link or a button. All these are performed with the help of click() method.

Thus a click() method typically works with elements like buttons and links.

Syntax 

driver.find_element_by_xpath("//button[id ='value']").click()

Example

Coding Implementation with click() method for clicking a link.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke #actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#to refresh the browser
driver.refresh()
# identifying the link then using click() method
driver.find_element_by_link_text("Company").click()
#to close the browser
driver.close()

Coding Implementation with click() method for clicking a button.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke #actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#to refresh the browser
driver.refresh()
# identifying the button then using click() method
driver.find_element_by_xpath("//button[contains(@class,'gsc-search')]") .click()
#to close the browser
driver.close()
raja
Published on 29-Jul-2020 11:18:43
Advertisements