I have tired to use xpath and css locator but both unable to click.
Can anyone help to see why the button element is different from the rest?
Code trial:
driver.findElement(By.xpath("//span[@id='a-autoid-2']")).click();
I have tired to use xpath and css locator but both unable to click.
Can anyone help to see why the button element is different from the rest?
Code trial:
driver.findElement(By.xpath("//span[@id='a-autoid-2']")).click();
The element is not a button
element but a <span>
tag associated with a DropDown looking like a button
due to the presence of class attributes a-button
, a-button-dropdown
and a-button-small
.
To click on the desired element you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("span.a-button.a-button-dropdown.a-button-small span.a-dropdown-prompt")).click();
xpath:
driver.findElement(By.xpath("//span[@class='a-button a-button-dropdown a-button-small']//span[@class='a-dropdown-label']")).click();
Use the below xpath instead, which have the button role rather the parent span.
//span[@id='a-autoid-2']//span[@role='button']
If you are trying to modify the quantity (Qty), try the below CSS Selector
driver.FindElement(By.CssSelector("#a-autoid-2 span[data-action='a-dropdown-button']")).Click();
There is an attribute for that drop down button which is [aria-pressed="false"]. This defaults to 'false' and once clicked the attribute changes to 'true'. Now WebDriver cannot find the Qty drop down as the above locator's attribute is changed. Try to use a fluent wait after your click in your code to fetch the updated DOM attributes.