[英]How to locate and click on a link having javascript within the href attribute through selenium java
[英]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.