繁体   English   中英

如何使用 Selenium 到 Java 通过 href 属性定位元素

[英]How to locate the element by href attribute using Selenium through Java

我正在尝试使用href定位以下元素。

<a class="compliance-documents-link-text" target="_blank" href="api/Document/GetDocumentByType/25194300-f620-4cc9-a375-419b1ebfe729/1">
<span>Acceptance Criteria</span>
</a>

我必须使用href="api/Document/GetDocumentByType/25194300-f620-4cc9-a375-419b1ebfe729/1"因为我需要区分都有接受标准但文档链接不同的不同产品。

我目前有:

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@href ='api/Document/GetDocumentByType/8d844ee3-b9cc-4d5c-87ee-8363bf46164a/1']")));

但它无法定位元素。

您的 XPATH 不正确。

在您的代码中,您的 href 值末尾有 '419b1ebfe729/14' ,而您在最后提到它为 '419b1ebfe729/1' 。

所需的元素是动态元素,因此要在元素上定位和click() ,您必须为elementToBeClickable()诱导WebDriverWait ,您可以使用以下任一定位器策略

  • xpath 1:

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='compliance-documents-link-text' and starts-with(@href, 'api/Document/GetDocumentByType/')]//span[text()='Acceptance Criteria']"))).click();
  • xpath 2:

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='compliance-documents-link-text' and starts-with(@href, 'api/Document/GetDocumentByType/')][./span[text()='Acceptance Criteria']]"))).click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM