簡體   English   中英

selenium webdriver 找到錨標簽並點擊它

[英]selenium webdriver to find the anchor tag and click that

<div id="ContentPrimary">
<ul class="selectors modeSelectors">
    <li><a href="/content/l411846326l1213g/references/" title="">
        <span class="selector">References (27)</span></a></li>
    <li><a href="/content/l411846326l1213g/referrers/" title="">
        <span class="selector">Cited By (2)</span></a></li>
    <li><a href="/content/l411846326l1213g/export-citation/" title="">
        <span class="selector">Export Citation</span></a></li>
    <li><a href="/content/l411846326l1213g/about/" title="">
        <span class="selector">About</span></a></li>
</ul>

在此我需要使用 Selenium api 找到並單擊關於鏈接,但我無法做到。

我所做的是

wait.until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver webDriver) {
        System.out.println("Searching ...");
        String s = driver.findElement(By.cssSelector("#ContentPrimary ul li[4] span.selector")).getText();
        System.out.println(s);
        if (Pattern.compile(Pattern.quote("About"), Pattern.CASE_INSENSITIVE).matcher(s).find()) {
            return true;
        } else {
            return false;
        }
    }
});
driver.findElement(By.linkText("About")).click();

但它不起作用

根據我的經驗,Selenium API 在這方面有很多缺陷。 它們大多只能通過重新編寫選擇器來克服。 例如,您可以嘗試使用 XPath 選擇器來獲取您的元素:

driver.findElement(By.xpath("//a[contains(.,'About')]")).click();

此外,如果您嘗試使用 Internet Explorer,則不單擊該元素可能會有所幫助,而是模擬按下 Enter 按鈕。 所以假設找到了元素,你可以試試這個:

driver.findElement(By.linkText("About")).sendKeys(Keys.ENTER);

您可以使用ExpectedConditions

wait.until(visibilityOfElementLocated(By.linkText("About"))).click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM