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