簡體   English   中英

使用硒在頁面之間導航-Java

[英]navigate between pages with selenium - Java

我瀏覽了一個鏈接列表,我一個一個地單擊它們,轉到鏈接頁面並意識到需要執行的操作,然后返回列表以單擊下一個鏈接,它運行良好。

我現在需要的是鏈接的結尾,循環結束,硒單擊“前進”按鈕轉到下一頁,然后再次完成此頁面的鏈接計數,然后再次開始循環。

我不能讓硒單擊移動,因為它說click(); 命令不能在webelento中使用。

未定義類型List <WebElement>的click()方法

清單

這是HTML結構:

<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>

硒代碼:

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();

圖片

您的click函數不會出現,因為driver.findElements(By.linkText(“ next”))返回列表List<WebElement>並且無法在列表對象上調用click()。

您可以在整個列表中調用click方法:

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

應該是driver.findElement(By.linkText("next")).click(); driver.findElements返回List<WebElement>driver.findElement返回單個WebElement

另外,該按鈕似乎沒有next文本。 嘗試按班級看

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

next文本看起來像

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

next <a>結束標記之前。

我修改了代碼以遍歷google搜索結果頁面並獲取結果的URL。

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"));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM