簡體   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