- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is there a way to find an element by attributes in Python Selenium?
We can find an element by attributes with Selenium webdriver. There are multiple ways to do this. We can use the locators like css and xpath that use attributes and its value to identify an element.
For css selector, theexpression to be used is tagname[attribute='value']. There are two types of xpath – absolute and relative. The xpath expression to be used is //tagname[@attribute='value'], the tagname in the expression is optional. If omitted, the xpath expression should be //*[@attribute='value'].
Let us consider an element with input tagname. It will be identified with xpath locator with id attribute (//input[@id='txtSearchText']).
Example
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/tutor_connect/index.php") #identify element with attribute id l= driver.find_element_by_xpath("//input[@id='txtSearchText']") l.send_keys("Tutorialspoint") #get_attribute() to get value of input box print("Enter text is: " + l.get_attribute('value')) driver.close()
Output
- Related Articles
- Is there any way to load an extension in chrome browser using Selenium Webdriver?
- Find and click element by title Python Selenium.
- How to find the status of an element in a page in Selenium with python?
- Is there an easy way to rename a table in a MySQL procedure?
- Is there a way to name columns in an INSERT statement in MySQL?
- Is there a way to print all methods of an object in JavaScript?
- Find next sibling element in Selenium, Python?
- Is there any way to check if there is a null value in an object or array in JavaScript?
- Find Element and FindElements by XPath in Selenium
- How to find a radio button element by value using Selenium?
- Is there a way to list collections in MongoDB?
- How to find an element using the “XPath” in Selenium?
- Is there a standard way of using exception chains in Python 3?
- Is there any way to embed a PDF file into an HTML5 page?
- How to identify the elements by partially comparing to its attributes in Selenium?

Advertisements