Simple input[name=Sex][value=M] would do pretty nice. And it's actually well-described in the standard doc:
Multiple attribute selectors can be used to refer to several attributes of an element, or even several times to the same attribute.
Here, the selector matches all SPAN elements whose "hello" attribute has exactly the value "Cleveland" and whose "goodbye" attribute has exactly the value "Columbus":
span[hello="Cleveland"][goodbye="Columbus"] { color: blue; }
As a side note, using quotation marks around an attribute value is required only if this value is not a valid identifier.
JSFiddle Demo
Answer from raina77ow on Stack OverflowHi all. I'm trying to use Selenium to find an element by CSS selector and although I've tried using the documentation, I think I'm misunderstanding something.
This is what I'm trying:
driver.find_element(By.CSS_SELECTOR,f"img [src='images/icons/new.gif'][alt='Add new item to list']").click()
This is the element I want to find and click:
<img src="images/icons/new.gif" alt="Add new item to list">
The error I get is as follows:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"img [src='images/icons/new.gif'][alt='Add new item to list']"}I can see it's on the page, however, so I'm not sure what I'm missing. I can find other items using XPATH and ID so selenium is up and running, just to rule that out. Can anybody suggest anything?
(I'm using selenium 4.8.2 and python 3.11.1, if that helps)
Simple input[name=Sex][value=M] would do pretty nice. And it's actually well-described in the standard doc:
Multiple attribute selectors can be used to refer to several attributes of an element, or even several times to the same attribute.
Here, the selector matches all SPAN elements whose "hello" attribute has exactly the value "Cleveland" and whose "goodbye" attribute has exactly the value "Columbus":
span[hello="Cleveland"][goodbye="Columbus"] { color: blue; }
As a side note, using quotation marks around an attribute value is required only if this value is not a valid identifier.
JSFiddle Demo
For concatenating it's:
input[name="Sex"][value="M"] {}
And for taking union it's:
input[name="Sex"], input[value="M"] {}
How to find_elements with multiple attributes using cssSelector Python and Selenium - Stack Overflow
java - How to select an element with multiple CSS attributes using Selenium Webdriver - Stack Overflow
java - How do I use a combination of attributes to select a button in Selenium? - Software Quality Assurance & Testing Stack Exchange
how to target html nested elements ?
The HTML you show has no ID (it's not the href that has a # btw, that's different).
so
findElement(By.Xpath(".//*[@id='step1']/button"))`
will not work based on what you posted.
You could use:
Css is always my first go to for readability:
findElement(By.Css(".btn-next[data-next='true']"))
or by Class:
findElement(By.Class("btn-next"))
or by XPath
findElement(By.XPath("//*[@data-next]"))
or your issue may be that multiple exist and you need multiple qualifiers:
by Xpath example:
findElement(By.XPath("//*[@data-next][@class='btn-next']"))
etc.
Based on your new update with the HTML you could use:
By.Css("form div#step1 button.btn-next")
if it exists twice you might need:
By.Css("form div#step1 ul.buttons button.btn-next")
or
By.Css("form div#step1 ul.buttons button.btn-next:first-of-type")
You might need to put the first-of-type on the form, the div, the ul, etc.
as you can see the pattern with css is to have the elements and any qualifiers in a short compact format. it depends on the rest of the page and if it has the stuff repeated
Try this xpath -
"//button[@class='btn-next'][.='Next']"
Is the button visible when the selector is fired? If not first verify with ExpectedConditions if it is visible or clickable.