元素:
<a href="https://rads.stackoverflow.com/amzn/click/com/B071NZZHF9" rel="nofollow noreferrer"> == $0
"
B071NZZHF9
"
尝试:
WebElement element = dr.findElement(By.xpath("//a[text()='B071NZZHF9']"));
并尝试过:
WebElement element = dr.findElement(By.xpath("//a[@href='http://www.amazon.com/gp/product/B071NZZHF9']"));
但是出现了错误:无法找到元素而无法单击元素
答案 0 :(得分:0)
根据您共享的HTML
点击您可以使用的WebElement
:
dr.findElement(By.xpath("//a[@href='https://rads.stackoverflow.com/amzn/click/B071NZZHF9']")).click();
答案 1 :(得分:0)
您可以使用此xpath并应用等待并尝试
//a[contains(text(), 'B071NZZHF9')]
使用显式等待尝试此代码:
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();