15
<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>

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

what I did is

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

but its not working

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
Sathish Kumar k k
  • 1,732
  • 5
  • 26
  • 45
  • 3
    Please describe what is not working exactly, what browser are you using, and whether an exeption is thrown etc... – devsnd May 27 '12 at 12:35
  • Unrelated to the question, but could helpful for you for your future. You can write the last condition more directly: `return Pattern.compile(Pattern.quote("About"), Pattern.CASE_INSENSITIVE).matcher(s).find()` – Petr Janeček May 27 '12 at 22:43

3 Answers3

26

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

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);
devsnd
  • 7,382
  • 3
  • 42
  • 50
2

You can use ExpectedConditions:

wait.until(visibilityOfElementLocated(By.linkText("About"))).click();
Aleh Douhi
  • 1,958
  • 1
  • 14
  • 13
0

Try using xpath to find the ancor then use that object to send an enter key

anchorObj.sendKeys(Keys.ENTER);

Thakhani Tharage
  • 1,288
  • 16
  • 19