How to select the text of a span on click in Selenium?

SeleniumAutomation TestingTesting Tools

We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname.

After identification of the element, we can perform the click operation on it with the help of the click method. Then obtain its text with the text method. Let us investigate the html code of a webelement with a span tag.

Example

from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#identify element and enter text
e = driver.find_element_by_class_name("search")
e.send_keys("tutorialspoint@gmail.com")
l = e.get_attribute('value')
print("Text entered: ")
print(l)
#identify span element then click and obtain text
s = driver.find_element_by_css_selector("span[class='input_group_button']")
s.click()
t = s.text
print("Text of element with span: ")
#quit browser
driver.quit()

Output

raja
Published on 08-Apr-2021 07:28:38
Advertisements