amlan dey amlan dey - 5 months ago 39x
Java Question

How to differentiate between image link and href link in selenium webdriver?

I am trying to get the name of the links of wikipedia home page in selenium webdriver . In the home page there is a table at the bottom which contains the link of wikipedia sister projects like Media-wiki, meta wiki etc . But after running the code I am getting 24 links. But in the webpage there are only 12 links. My suspicion is it is taking the links of images also.

package tcsWebmail;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WikiPediaLinks {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

WebElement Block=driver.findElement(By.xpath("//*[@id='mp-sister']/table//a[not(img)]"));

List<WebElement> Links= Block.findElements((By.tagName("a")));
System.out.println("Printing the no of items in block");
int i=0;
for ( i=0;i<Links.size();i++){
System.out.println("The no of items are"+Links.size());


Your XPath includes images as you suspect. In order to get a that don't contain a descendant img, you can to use XPath below:



//*[@id='mp-sister']/table//a[not(descendant::*[local-name() = 'img'])]

See code below:

List<WebElement> Links= driver.findElements(By.xpath("//*[@id='mp-sister']/table//a[not(img)]"));