简体   繁体   中英

navigate between pages with selenium - Java

I walk a list of links , I click on them one by one , I go to the page the link and realize the actions that need to perform and then return to the list to click on the next link, it is working perfectly.

What I need now is to come to the end of the links , where the loop ends , the selenium click the forward button to go to the next page and be done again the link count of this page and start the cycle again.

I can not make the selenium click the move because it says that the click(); command You can not be using in a webelento .

The method click () is undefined for the type List < WebElement >

清单

This is the HTML structure:

<div id="results-pagination">

<h2 id="pagination-heading">Pagination</h2>

    <ul class="pagination">

        <li class="prev">
            <a class="page-link" href="url" title="back" data-li-page="1">&lt; back</a>
        </li>

        <li class="link">
            <a class="page-link" href="url" title="page 2" data-li-page="2">2</a>
        </li>

        <li class="next">
            <a class="page-link" href="next" title="next" data-li-page="next"></a>
        </li>

    </ul>
</div>

selenium code:

List<org.openqa.selenium.WebElement> numberpages= driver.findElements(By.className("page-link"));
            System.out.println("numberpages : " + numerospaginas.size());

            List<org.openqa.selenium.WebElement> links= driver.findElements(By.linkText("to connect"));
            System.out.println("Count to connect : " + links.size());

            Thread.sleep(2000);

            for(int i=0;i<5;i++){
            links= driver.findElements(By.linkText("to connect")); 
            links.get(i).click();
            Thread.sleep(2000); 
            boolean convite = driver.getPageSource().contains("iweReconnectSubmit");

            if(invite == true){

                Thread.sleep(2000); 

                boolean error = driver.getPageSource().contains("message:");

                do{
                //action
                By tipoPlano = By.cssSelector("[name='reason'][value='IF'][type='radio']");
                driver.findElement(tipoPlano).click();
                }while(error == true);      

                //submit
                driver.findElement(By.name("iweReconnectSubmit")).click();
                Thread.sleep(2000);

                WebDriverWait confirmacaoadicao = new WebDriverWait(driver, 10);  
                confirmacaoadicao.until(ExpectedConditions.textToBePresentInElement(By.id("control_gen_3"), "invite for: "));


                String pessoa = driver.findElement(By.xpath("//div[@id='control_gen_3']//a")).getText();               
                System.out.println(pessoa + " add" );   

                driver.navigate().to(list_of_links);

                WebDriverWait retorno = new WebDriverWait(driver, 10);
                retorno.until(ExpectedConditions.elementToBeClickable(By.linkText("To connect")));

            } 
            }

//does not work
driver.findElements(By.linkText("next")).click();

//does not work
((org.openqa.selenium.WebElement)driver.findElements(By.linkText("next"))).click();

图片

your click function is not coming because driver.findElements(By.linkText("next")) returns a list List<WebElement> and click() cant be called on a list object .

you can call click method my iterating over the list :

List<WebElement> WebElementList = driver.findElements(By.linkText("next")); 
        for(WebElement element : WebElementList){
            element.click(); // click can be called on object of WebElement
        }

It should be driver.findElement(By.linkText("next")).click(); . driver.findElements returns List<WebElement> while driver.findElement returns single WebElement .

Also, it seems the button doesn't have next text. Try looking by class

driver.findElement(By.className("next")).click();

next text will look like

<a class="page-link" href="next" title="next" data-li-page="next">"next"</a>

with next before the <a> closing tag.

I modified the code to iterate through google search results pages and to get the results' URLs.

public static void searchGoogle(String query) throws InterruptedException {
    try {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.co.uk");

        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("\"" + query + "\" filetype:pdf\n");
        element.submit();

        // wait until the google page shows the result
        WebElement myDynamicElement = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));

        getResults(driver);
        Thread.sleep(1000);

        for (int i = 0; i < 10; i++) {
            driver.findElement(By.linkText("Next")).click();
            Thread.sleep(1000);
            getResults(driver);
        }
    } catch (Exception e) {
        System.err.println("Error caught - " + e);
    }

}

public static void getResults(WebDriver driver) {
    List<WebElement> findElements = null;
    findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a"));

    for (WebElement webElement : findElements) {
        System.out.println(webElement.getAttribute("href"));
    }
}

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