You can easily accomplish this task with CSS.
The formula is:
element[attribute='attribute-value']
So if you have,
<a href="mysite.com"></a>
You can find it using:
By.cssSelector("a[href='mysite.com']");
this works using any attribute possible.
This page here gives good information on how to formulate effective css selectors, and matching their attributes: http://ddavison.io/2014/02/18/effective-css-selectors
Answer from ddavison on Stack OverflowYou can easily accomplish this task with CSS.
The formula is:
element[attribute='attribute-value']
So if you have,
<a href="mysite.com"></a>
You can find it using:
By.cssSelector("a[href='mysite.com']");
this works using any attribute possible.
This page here gives good information on how to formulate effective css selectors, and matching their attributes: http://ddavison.io/2014/02/18/effective-css-selectors
I do not understand your requirement:
Assuming XPath is not an option ...
If this was just an incorrect assumption on your part, then XPath is the perfect option!
webDriver.findElements(By.xpath("//element[@attribute='value']"))
Of course you need to replace element, attribute, and value with your actual names. You can also find "any element" by using the wildcard:
webDriver.findElements(By.xpath("//*[@attribute='value']"))
Python - Selenium get Element by attribute and the full attribute value - Stack Overflow
Is there a way to find an element by attributes in Python Selenium? - Stack Overflow
[Python 3 + Selenium] - Finding an element by xpath and text in the href attribute.
Is there a list of attributes that can be specified with Selenium get_attribute()?
Videos
While constructing locators considering css-selectors or xpath you have to use the different attributes and the attribute-values to identify the WebElement uniquely within the DOM Tree.
The generic way is:
Using css_selector:
button.classname[attributeA='attributeA_value'][attributeB='attributeB_value']Using xpath and attributes:
//button[@attributeA='attributeA_value'][@attributeB='attributeB_value']
As an example, for an element like:
<button type="button" aria-hidden="true" class="close alert alert-close" data-notify="dismiss">Close</button>
You can identify the Close element using either of it's the attributes and the corresponding attribute-values using either of the Locator Strategies:
Using css_selector:
button.close.alert.alert-close[data-notify='dismiss'] classes-> ^ ^^ ^^^ data-notify ^^^^ attributeUsing xpath and attributes:
//button[@class='close alert alert-close' and @data-notify='dismiss'] class attributes ^ ^^ ^^^ data-notify ^^^^ attributeUsing xpath and innerText:
//button[text()='Close'] innerText ^
The CSS selectors would be formatted like this:
'[attribute]'
'[attribute="value"]'
For example, the selector for the input field on google.com would be:
'input[name="q"]'
You can get it by xpath and check the node-type attribute value:
driver.find_element_by_xpath('//input[@node-type="searchInput"]')
Even though the question is old but it's still very relevant I believe. You may be able to use simple css selector and the syntax is standard javascript similar to jquery or native browser support.
driver.find_element_by_css_selector('span.className[attrName="attrValue"]')
Example:
driver.find_element_by_css_selector('span.blueColor[shape="circle"]')