Find and click element by title Python Selenium.

SeleniumAutomation TestingTesting Tools

We can find and click elements by title in Selenium webdriver. An element can be identified with a title attribute with xpath or css selector. With the xpath, the expression should be //tagname[@title='value']. In css, the expression should be tagname[title='value'].

Let us take an html code for an element with a title attribute.

The xpath to identify the element should be //a[@title='Tutorialspoint'] and the css expression should be a[title='Tutorialspoint']. Once the element is identified we can use the click() method for clicking on it.

Example

Code Implementation.

from selenium import webdriver
driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe")
# implicit wait for 5 seconds
driver.implicitly_wait(5)
# maximize with maximize_window()
driver.maximize_window()
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
# identify element with title attribute and click()
l=driver.find_element_by_xpath("//a[@title='Tutorialspoint']")
l.click()
print("Current page title: " + driver.title)
driver.quit()
raja
Published on 28-Aug-2020 13:18:53
Advertisements