Please help-Module 13,Clicking on links in bulk | Selenium Forum
M
Posted on 21/09/2015
Hello All,
I was working on the Module 13 in which you click on multiple links,get the text and the title of the page.I followed the tutorial but my code is just clicking on the first link and printing the title of that page.After that,it is not clicking on the next link and there are no errors at all.I was using http://bbc.com

Further details

Issue - Clicking on links in Bulk @BBC.com -there are 6 links,the code is clicking on the first and prints the text of the link and gets the title of the current page.After that,it is not iterating for the rest of the links.

Expected output-Should click on all the 6 links and print the text of the links along with the title of the page

Actual output-Working for the first link only.

Below is the code:
import org.apache.http.client.fluent.Request;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Specificlinks_Second {
static WebDriver driver;
public static void main(String[] args) {
driver=new FirefoxDriver();
driver.get("http://www.bbc.com/sport/0/");
//here we have found five links which have the similar pattern so we divided the xpath on thebasis of that pattern
String part1="//*[@id='programme-links-hyper']/ul/li[";
String part2="]/a";
// for(int i=1;i<=6;i++)
// Now here we will declare the while loop from which we will call teh iselemntpresent function
int i=1;
while(iEelementPresent(part1+i+part2))
{
String text=driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
String url=driver.findElement(By.xpath(part1+i+part2)).getAttribute("href");
if(checkresponse(url))
{


driver.findElement(By.xpath(part1+i+part2)).click();
System.out.println(driver.getTitle());
driver.get("http://www.bbc.com/sport/0/");
//driver.navigate().back();
}else{
System.out.println("Something is wrong");
}
i++;
}
}

//we will make a function which will return true if the element with the given xpath is present or false if the element is not present
public static boolean iEelementPresent(String elementxpath)
{
int count=driver.findElements(By.xpath(elementxpath)).size();
if(count==0)
{
return false;

}else
{
return true;
}


}

public static boolean checkresponse(String url)
{
try {
int resp_code= Request.Get(url).execute().returnResponse().getStatusLine()
.getStatusCode();
if(resp_code==200){
return true;
}else{
return false;
}

} catch (Exception e) {
return false;
}
}


}
Here are the xpaths of those 6 links
//*[@id='programme-links-hyper']/ul/li[1]/a
//*[@id='programme-links-hyper']/ul/li[2]/a
//*[@id='programme-links-hyper']/ul/li[3]/a
//*[@id='programme-links-hyper']/ul/li[4]/a
//*[@id='programme-links-hyper']/ul/li[5]/a
//*[@id='programme-links-hyper']/ul/li[6]/a

What is wrong with this code?

M
Replied on 22/09/2015

added some waits. got rid of check response it was not working in my computer.


[code:2u9pnv8n]
import org.junit.runner.Request;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Specificlinks_Second {
static WebDriver driver;

public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
driver.get("http://www.bbc.com/sport/0/");
// here we have found five links which have the similar pattern so we
// divided the xpath on thebasis of that pattern
String part1 = "//*[@id='programme-links-hyper']/ul/li[";
String part2 = "]/a";
// for(int i=1;i<=6;i++)
// Now here we will declare the while loop from which we will call teh
// iselemntpresent function
int i = 1;
while (iEelementPresent(part1 + i + part2)) {
String text = driver.findElement(By.xpath(part1 + i + part2)).getText();
System.out.println(text);
String url = driver.findElement(By.xpath(part1 + i + part2)).getAttribute("href");
System.out.println(url);
if (checkresponse(url)) {
driver.findElement(By.xpath(part1 + i + part2)).click();
System.out.println(driver.getTitle());
// driver.get("http://www.bbc.com/sport/0/");
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
} else {
System.out.println("Something is wrong");
}
Thread.sleep(2000);
i++;
}
}

// we will make a function which will return true if the element with the
// given xpath is present or false if the element is not present
public static boolean iEelementPresent(String elementxpath) {
int count = driver.findElements(By.xpath(elementxpath)).size();
if (count == 0) {
return false;

} else {
return true;
}

}

public static boolean checkresponse(String url) {
try {
// int resp_code =
// Request.Get(url).execute().returnResponse().getStatusLine().getStatusCode();
// if (resp_code == 200) {
// return true;
// } else {
// return false;
// }
return true;
} catch (Exception e) {
return false;
}
}

}[/code:2u9pnv8n]