微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – selenium webdriver找到锚标签并单击它

<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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐