-4

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?

Here is the URL

Code trial:

driver.findElement(By.xpath("//span[@id='a-autoid-2']")).click();
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
user2201789
  • 115
  • 9

3 Answers3

1

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();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    this works. so i need combination of locators...still learning and not quite sure how can i get and combine the right one. Thanks! – user2201789 May 03 '19 at 01:40
0

Use the below xpath instead, which have the button role rather the parent span.

//span[@id='a-autoid-2']//span[@role='button']
supputuri
  • 12,238
  • 2
  • 14
  • 34
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@id='a-autoid-2']//span[@role='button']"} – user2201789 May 03 '19 at 01:34
0

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.

KunduK
  • 26,790
  • 2
  • 10
  • 32
Deepak
  • 13
  • 1
  • 4