Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97796

Parse links if pagination exists, using Selenium

$
0
0

I have modified my script and now it is running smooth and fine.

What I have done, thanks to SO support.

  1. Open URL as www.my.url

  2. Open all 20 links one by there and saving the Names

But, what I noticed, that www.my.url has 20 pages (pagination) So, current way of script is working only for 1st pagination My code is here

public class GetAllLinks {

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

        System.setProperty("webdriver.chrome.driver", "C://Dell//chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.reklama.lv/ru/manufacture/build-works/uslugi/table.html");
        driver.manage().window().maximize();

        //Get list of web-elements with tagName  - a
        driver.findElement(By.xpath("//em[@class='view1']")).click();
        List<WebElement> demovar = driver.findElements(By.xpath("//*[@id=\"big_icon_view\"]/ul/li/p/a"));
        System.out.println(demovar.size());

        ArrayList<String> hrefs = new ArrayList<String>(); //List for storing all href values for 'a' tag
        for (WebElement var : demovar) {
            System.out.println(var.getText()); // used to get text present between the anchor tags
            System.out.println(var.getAttribute("href"));
            hrefs.add(var.getAttribute("href"));
        }

        int i = 0;
        for (String href : hrefs) {
            driver.navigate().to(href);
            boolean isPresent = driver.findElements(By.xpath("//h3[@id='NameTitle']")).size() > 0;
            if (isPresent) {
                String test = driver.findElement(By.xpath("//*[@id=\"NameLink\"]")).getText();
                System.out.println(test);
            } else {
                System.out.println("No name found");
            }
            Thread.sleep(3000); // To check if the navigation is happening properly.
        }
    }
}

As I mentioned before, it will parse all links which are visible on 1st page. But, I also need to implement pagination.

So, I tried to adding a pagination here

for (WebElement var : demovar) {
            System.out.println(var.getText()); // used to get text present between the anchor tags
            System.out.println(var.getAttribute("href"));
            hrefs.add(var.getAttribute("href"));
        }

and was adding something like

    if(driver.findElement(By.xpath("//paginationButton")).isDisplayed() {
                driver.findElement(By.xpath("//paginationButton")).click();
     System.out.println(var.getText()); // used to get text present between the anchor tags
                    System.out.println(var.getAttribute("href"));
                    hrefs.add(var.getAttribute("href"));
            } else {
System.outprintln("no more pagination button");
}

Assuming, that on second page, script will check other 20 links, and open them one by one. But, unfortunatelly here I'm failing.


Viewing all articles
Browse latest Browse all 97796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>