selenium webdriver找到锚标签并单击它 [英] selenium webdriver to find the anchor tag and click that

查看:145
本文介绍了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的关于链接但是我无法做到。

In this I need to find and click About link using Selenium api but I was unable to do it.

我做的是

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选择器来获取元素:

In my experience the Selenium API has many flaws in that way. They can mostly only be overcome by reformulating your selectors. For example you could try using a XPath selector to get your element:

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

此外,如果您尝试使用Internet Explorer,可能有助于不点击该元素,但而是模拟按下Enter按钮。
所以找到元素,你可以试试这个:

Also, if you are trying to use the Internet Explorer it might help not to click the element, but instead to simulate pushing the Enter button. So assumung the Element is found, you could try this:

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

这篇关于selenium webdriver找到锚标签并单击它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆