Error while click on the link and get the title of each link | Selenium Forum
M
Posted on 04/11/2015
Hi ,
i am facing an issue when click on the links on the particular area eg --footer
Plz find the screen shot and code

public class redif2 {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.askme.com/");
WebElement box=driver.findElement(By.xpath("html/body/div[2]/div[4]/div[2]/div/div[1]/div[1]/ul[1]"));
List<WebElement> allLinks=box.findElements(By.tagName("a"));
System.out.println("Total Link--"+allLinks.size());
for(int i =1;i<allLinks.size();i++){
System.out.println("------------");
System.out.println(allLinks.get(i).getText());
allLinks.get(i).click();
System.out.println(driver.getTitle());
driver.get("http://www.askme.com/");
driver.findElements(By.xpath("html/body/div[2]/div[4]/div[2]/div/div[1]/div[1]/ul[1]]"));
allLinks=box.findElements(By.tagName("a"));
}

}

}

M
Replied on 05/11/2015

[color=#FF0000:2wfnl1br][quote:2wfnl1br]invalid selector exception[/quote:2wfnl1br] [/color:2wfnl1br]

Thrown when the selector which is used to find an element does not return a WebElement. Currently this only happens when the selector is an xpath expression and it is either syntactically invalid (i.e. it is not a xpath expression) or [color=#FF0000:2wfnl1br]the expression does not select WebElements[/color:2wfnl1br] (e.g. “count(//input)”).


[quote:2wfnl1br][color=#FF0000:2wfnl1br]html/body/div[2]/div[4]/div[2]/div/div[1]/div[1]/ul[1][/color:2wfnl1br][/quote:2wfnl1br]

so, this xpath is all wrong. it does not select WebElements.


M
Replied on 06/11/2015

okay...
but When i moves the cursor on the links where all the Links covered(PFA ) and i see the Xpath in this form -->html/body/div[2]/div[4]/div[2]/div/div[1]/div[1]/ul[1]
and by using this it dispaly me very first link correctly but to fetch another it show the above error

Please let me know how can i use correct xpath so that i can extract all the link.

Responsive image

M
Replied on 06/11/2015

try to use [color=#FF0000:3qx5u3te]findelements [/color:3qx5u3te]it might work.


M
Replied on 08/11/2015

I have used the below code to run it successfully. If you do not mind then try the below code.

public static void main(String[] args) {
// TODO Auto-generated method stub
List<WebElement> askmeFooterLinks = new ArrayList<WebElement>();
//WebDriver driver = new FirefoxDriver();

// just change the path
System.setProperty("webdriver.chrome.driver","F:\\Testing\\QTPselenium\\Chrome_exe\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://askme.com");
WebElement footer = driver.findElement(By.xpath("//div[@class='footer-link-container']/div[1]/ul[1]"));

askmeFooterLinks = footer.findElements(By.tagName("a"));
System.out.println("Size of the list ----- "+askmeFooterLinks.size());
for(int i = 0; i < askmeFooterLinks.size();i++){
System.out.println(askmeFooterLinks.get(i).getText());
askmeFooterLinks.get(i).click();
System.out.println(driver.getTitle());
driver.get("http://www.askme.com/");
footer = driver.findElement(By.xpath("//div[@class='footer-link-container']/div[1]/ul[1]"));

askmeFooterLinks = footer.findElements(By.tagName("a"));
}
//driver.close();
//driver.quit();

}


M
Replied on 08/11/2015

Thanks ashwani it working fine Responsive image