简体   繁体   中英

Get all the non-hidden links in a webpage and click using Selenium Webdriver

How can I get all the links in the webpage and click (hidden links should be eliminated) using Selenium WebDriver?

I would like to click the links which are visible on the page.

public  void linksclick() throws Exception{ 

    System.setProperty("webdriver.chrome.driver", "path of chrome driver");
    WebDriver driver = new ChromeDriver();
    baseUrl = "www.example.com";
    driver.get(baseUrl);
    //driver.get(baseUrl);
    List<WebElement> allLinks = driver.findElements(By.tagName("a"));
    System.out.println("All Links--> " + allLinks.size());
    //Fetching an nth Link
    System.out.println(" 1st Link is-->  " + allLinks.get(1).getText());
    //Fetching all the Links


    for (int i=0;i<allLinks.size();i++){
        String homeWindow;
        String currentWindow;
        List<WebElement> allLinks1 = driver.findElements(By.tagName("a"));
        Thread.sleep(2000);
        System.out.println(allLinks1.get(i).getText()); 
        if((allLinks1.get(i).getText() == null | (allLinks1.get(i).getText()).equalsIgnoreCase("")|allLinks1.get(i).getAttribute("href") == null | allLinks1.get(i).getCssValue("font-size")==null | allLinks1.get(i).getCssValue("font-size")==null | allLinks1.get(i).getCssValue("font-size") == null
                   | allLinks1.get(i).getCssValue("font-weight")== null | allLinks1.get(i).getCssValue("text-align")== null 
                   | allLinks1.get(i).getCssValue("font-family")== null | allLinks1.get(i).getCssValue("color")== null))
                 {//DO NOTHING
        }

        else{
        Thread.sleep(2000);
        System.out.println(allLinks1.get(i).getText()); 
        //Thread.sleep(1000);
        homeWindow = driver.getWindowHandle().toString();
        allLinks1.get(i).click();
        Thread.sleep(2000);
        currentWindow = driver.getWindowHandle().toString();
        if(homeWindow.contentEquals(currentWindow)){

            //Do nothing

            }else {
                driver.close();
                driver.switchTo().window(homeWindow);
            //closechild window
            //focus on homewindow

            }
        driver.navigate().to(baseUrl);  
        }
    }
}

Please help me with the solution.

You can try using something like this:

if (allLinks1.get(i).isDisplayed())
{
    allLinks1.get(i).click();
}

If that doesn't do what you want, you can include allLinks1.get(i).isEnabled() in the if statement above.

There are a couple of problems with your code that you need to fix for it to work.

Make sure your url is actually valid: " http://www.example.com " instead of just "www.example.com".

Lists use a zero-based index. So instead of allLinks.get(1) , use allLinks.get(0) . Since there is only one link on the site, and your code is trying to get the second link in the list, you would have gotten an IndexOutOfBoundsException.

That should help your code to run.

A word of advice: give you variables more descriptive names so that your code helps comment itself. For example, it looks like the reason you make a new list of all the links in every iteration of the loop is so you won't have stale elements. So, instead of naming that variable allLinks1 , name it something like freshListOfAllLinks , so it's more obvious what your code is doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM