Module 14: Error while running the code for getting links an a page in Chrome | Selenium Forum
A
Abhilash Hariharan Posted on 21/06/2020

Hi Ashish,

I was trying to run the below code on Chrome browser. I was able to run the program successfully using breakpoints in the Debug mode and it completed successfully. However in the run mode it is always failing due to some error. Please find the code and the stack trace attached. Also in Firefox it runs in both Debug and Run mode without issues.

 

Regards,

Abhilash

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class CNN_Specific_Links2 {

public static void main(String[] args) throws InterruptedException {

//Disable the logs
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");

//Initialize an object of the WebDriver interface
WebDriver driver = new ChromeDriver();

//Maximize the window
driver.manage().window().maximize();

//Implicit wait - dynamic wait
//applicable to all driver.findElement commands
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

//Get the URL under test
//driver.get("http://cnn.com");
driver.navigate().to("http://cnn.com"); //remember browser history

//Break the xpath into 2 parts
String part1 = "//*[@id='intl_homepage1-zone-3']/div[5]/div/div[3]/ul/li[";
String part2 = "]/article/div/div[2]/h3/a/span[1]";

//Click on each of the links and print the text
for(int i = 1;i <= 5;i++) {
String text = driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
driver.findElement(By.xpath(part1+i+part2)).click();
System.out.println(driver.getTitle());
driver.navigate().back();
}
}

}


S
Support Team Replied on 23/06/2020

On which line is it failing?


A
Abhilash Hariharan Replied on 24/06/2020

The error is occuring at line 40 as given below:

 

driver.findElement(By.xpath(part1 + i + part2)).click();

 

Latest stack trace attached


A
Abhilash Hariharan Replied on 24/06/2020

I even checked in google from the stack overflow site and tried different options but no luck still. Issue is only in Chrome.


S
Support Team Replied on 25/06/2020

It gives you error for very first link or second one?