2

I am using selenium java 3.141.59 and testng 6.14.3.
The test page may display like

<tbody>
  <tr>
    <td class="S_line1">
      <strong class="W_f12">82</strong>
      <span class="S_txt2">fans</span>
    </td>
  </tr>
</tbody>

or

<tbody>
  <tr>
    <td class="S_line1">
      <a bpfilter="page_frame" class="t_link S_txt1" href="//xx.com/p/1003061291477752/follow?from=page_100306&amp;wvr=6&amp;mod=headfollow#place">
        <strong class="W_f12">170</strong>
        <span class="S_txt2">fans</span>
      </a>
    </td>
  </tr>
</tbody>

If "fans" have a href link,then I will click "fans" link. If not I will skip this step and continue to do others.
ExpectedConditions.presenceOfElementLocated is not availabe to this situation, because it will throw exeception when not finding href link and stop the test.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Venus
  • 1,184
  • 2
  • 13
  • 32
  • Possible duplicate of [WebDriver: check if an element exists?](https://stackoverflow.com/questions/6521270/webdriver-check-if-an-element-exists) – Sers Feb 11 '19 at 10:26
  • @Sers this is a bit different... OP is looking for an element with `href`, not the classic "check if an element exists" – Moshe Slavin Feb 11 '19 at 10:35

3 Answers3

3

For checking the node href is present or not, you can use the below XPath to identify the a node because href is present inside of a(I'm assuming that the class is unique here, if not then use some other locator with //a appending at the end) :

String xpath = "//td[@class=\"S_line1\"]/a"

You can check for it's presence like below :

List<WebElement> hrefs = driver.findElements(By.xpath(xpath));
if(hrefs.size() > 0) {
    System.out.println("=> The href is present...");
    hrefs.get(0).click();
} else {
    System.out.println("=> The href is not present...");
}

The above code will not throw any error if the href is not there. So you don't need to handle any exceptions there.

I hope it helps...

Ali
  • 1,689
  • 1
  • 5
  • 12
  • this is a good approach too... see my answer which will find only the linkText of `fans`... what do you think? – Moshe Slavin Feb 11 '19 at 12:27
  • 1
    Yes @Moshe Slavin, but it will throw an error if the expected value is not present and you are wrapping it with `try-catch` block which is known option so I have tried differently - Your's is still good approach... – Ali Feb 11 '19 at 12:32
1

Following code first finds all the <a> tags within the table, and one by one if tag have href, will click them:

List<WebElement> allAnchorElements = driver.findElements(By.xpath("//table//td[@class='S_line1']/a"));

for(WebElement currElem : allAnchorElements ){

     if(currElem.getAttribute("href")){

           currElem.click();
     }

}
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
0

You are looking for only the fans element that has the href...

The best way I can think of is to use By.linkText().

In your case:

try{
    WebElement link  = driver.findElement(By.linkText("fans"));
    System.out.println(link.getAttribute("href"));
    link.click();
    }
catch(NoSuchElementException e){
        System.out.println(e);
    }

Hope this helps you!

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38