
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Locate Elements in Span Class Using Python and Selenium
We can locate elements in span class and not unique id with the help of the Selenium webdriver. We can identify an element having a class attribute with the help of the locator xpath, css or class name.
To locate elements with these locators we have to use the By.xpath, By.xpath or By.cssSelector method. Then pass the locator value as a parameter to this method.
Let us see the html code of a button having a span class and try to identify it.
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") l = driver.find_element_by_id("textemail") l.send_keys("[email protected]") #get value entered s = l.get_attribute('value') #identify element with span class m = driver.find_element_by_xpath("//span[@class='input_group_button']") #verify if element present b = m.is_displayed() if b: print("Element with span class available") else: print("Element with span class not available") #close browser driver.close()
Output
Advertisements