0

I'm trying to open each link on the given web page in a new tab But by clicking two links, it throws this error. Any suggestions would help. Thanks.

Here is my code:

public class FirstClass {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver",
                "chromedriver");

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().deleteAllCookies();

        driver.get("https://www.google.com/");

        Actions action = new Actions(driver);

        List<WebElement> web = driver.findElements(By.tagName("a"));

        for (int i = 1; i < web.size(); i++) {

            System.out.println(web.get(i).getText());
            action.moveToElement(web.get(i)).keyDown(Keys.CONTROL).click().build().perform();
        }

    }

}

Here is an error which I got -

Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
Manik
  • 573
  • 1
  • 9
  • 28
Kaustubh
  • 506
  • 4
  • 22
  • 1
    Does this [discussion](https://stackoverflow.com/questions/59853448/javascript-error-failed-to-execute-elementsfrompoint-on-document-the-provi/59854498#59854498) helps you? – undetected Selenium May 19 '20 at 17:41
  • the element may have zero size... I would try just getting the href attributes... open a tab (execute script window.open()....), change window handle to new tab, then use .get there. – pcalkins May 19 '20 at 18:44
  • another alternative might be to check the size before doing that actions chain. – pcalkins May 19 '20 at 18:50
  • @DebanjanB: thanks that gave me the reason why that error occurs. – Kaustubh May 20 '20 at 06:01

1 Answers1

1

The code fails as some of the links in the Google Webpage are not displayed and can only be seen when certain actions are performed. The first link which results in failure of the code is "Report inappropriate predictions" link which can be seen when one clicks on the search bar.

To handle these links, one needs to traverse through the DOM when things get tricky. So, I added a JavaScriptExecutor to the code which comes into picture when a given link is not displayed.

Please use the below-mentioned code to open every link in the Google HomePage:

System.setProperty("webdriver.chrome.driver", "chromedriver");

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

driver.manage().deleteAllCookies();

driver.get("https://www.google.com/");

Actions action = new Actions(driver);

List<WebElement> web = driver.findElements(By.tagName("a"));

for (int i = 0; i < web.size(); i++) {
    if(web.get(i).isDisplayed()){
         action.moveToElement(web.get(i)).keyDown(Keys.CONTROL).click().build().perform();
    }else{
         String url = web.get(i).getAttribute("href");
         ((JavascriptExecutor)driver).executeScript("window.open('" + url + "', '_blank');");
     }
}

Hope this helps.

Satya
  • 576
  • 1
  • 6
  • 12