0

Element:

<a href="https://rads.stackoverflow.com/amzn/click/com/B071NZZHF9" rel="nofollow noreferrer"> == $0
   "

    B071NZZHF9

   "

Tried:

WebElement element = dr.findElement(By.xpath("//a[text()='B071NZZHF9']"));

And tried:

WebElement element = dr.findElement(By.xpath("//a[@href='http://www.amazon.com/gp/product/B071NZZHF9']"));

But got an error: Unable to locate element and unable to click the element

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

As per the HTML you have shared to click on the WebElement you can use :

dr.findElement(By.xpath("//a[@href='https://rads.stackoverflow.com/amzn/click/B071NZZHF9']")).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • tried using this code. but it does not work as it throws some error saying not able to click the element, rather some other element will be clicked – Anshul Srivastava Nov 08 '17 at 10:38
  • Great !!! So we have now solved our main issue. To address the `not able to click the element, rather some other element will be clicked` issue follow the steps in this [**`QA/Discussion`**](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el/44916498#44916498). Please Accept the Answer. – undetected Selenium Nov 08 '17 at 11:39
0

You can use this xpath and apply wait and try

//a[contains(text(), 'B071NZZHF9')]

Try this code with Explicit wait:

WebDriverWait waitForElement= new WebDriverWait (20,dr);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'B071NZZHF9')]")));
dr.findElement(By.xpath("//a[contains(text(), 'B071NZZHF9')]")).click();
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36