amlan dey amlan dey - 6 months ago 52
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.io.File;
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();
driver.get("https://en.wikipedia.org/wiki/Main_Page");
System.out.println(driver.getTitle());

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(Links.get(i).getText());
}
System.out.println("The no of items are"+Links.size());
driver.quit();
}
}

Answer

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(img)]

or

//*[@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)]"));
Comments