Module 13: Reading http response code is not working | Selenium Training|Java|WebDriver|TestNG|Frameworks|Videos

Selenium Training Forum

Module 13: Reading http response code is not working

D
dipu.sandeep Post on 10/10/2019

I have added fluent-hc-4.2.2. jar to my project in build path as external library but I am getting "The type org.apache.http.client.ClientProtocolException cannot be resolved. It is indirectly referenced from required .class files" on the highlighted line of code. Can you please suggest which is wrong here?

 

public static boolean getResponseCode(String url) {
int resp_code = 0;
try {

resp_code = Request.Get(url).execute().returnResponse().getStatusLine()
.getStatusCode();

System.out.println("Respose code for URL " + url + " is -> " + resp_code);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (resp_code == 200)
return true;
else
return false;
}

 

 

S

Instructor

Support Team Reply on 10/10/2019

Please export your project in a compressed zip file and share it with us so that we can suggest a solution.

D
dipu.sandeep Reply on 12/10/2019

Issue is CNN_Specific_Link2.java on line number 67,68

S

Instructor

Support Team 3 days go

Please share the updated code where you are calling the getResponseCode() method.

D
dipu.sandeep 2 days go

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CNN_Specific_Links2 {
static WebDriver driver;

public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Learning\\MySoftware\\drivers\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "C:\\Learning\\MySoftware\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
// implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
// applicatble to all driver.findelement command
// driver.get("https://www.cnn.com/");
driver.navigate().to("https://www.cnn.com/");
// for(int i=2;i<9;++i) {

String part1 = "//*[@id=\'homepage1-zone-1\']/div[2]/div/div[3]/ul/li[";
String part2 = "]/article/div/div/h3/a/span[1]";
int i = 2;
while (isElementPresent(part1 + i + part2, "xpath")) {
String text = driver.findElement(By.xpath(part1 + i + part2)).getText();
System.out.println("text is: " + text);
String url = driver.findElement(By.xpath(part1 + i + part2)).getAttribute("href");
System.out.println(url);
driver.findElement(By.xpath(part1 + i + part2)).click();
System.out.println("Title is :" + driver.getTitle());
driver.navigate().back();
i++;

}
}

public static boolean isElementPresent(String locator, String locatorType) {
List<WebElement> allElements = null;
if (locatorType.equalsIgnoreCase("xpath")) {
allElements = driver.findElements(By.xpath(locator));

} else if (locatorType.equalsIgnoreCase("cssSelector")) {
allElements = driver.findElements(By.cssSelector(locator));

} else if (locatorType.equalsIgnoreCase("id")) {
allElements = driver.findElements(By.id(locator));

} else if (locatorType.equalsIgnoreCase("name")) {
allElements = driver.findElements(By.name(locator));

}

if (allElements.size() == 0) {
return false;
}

return true;
}

public static boolean getResponseCode(String url) {
int resp_code = 0;
try {

 resp_code = Request.Get(url).execute().returnResponse().getStatusLine().getStatusCode();

System.out.println("Respose code for URL " + url + " is -> " + resp_code);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (resp_code == 200)
return true;
else
return false;
}

}

S

Instructor

Support Team 2 days go

I Analysed the code and everything is working fine.

Just download the complete jar binaries from the link below.

http://hc.apache.org/