使用selenium-Java在页面之间导航

使用selenium-Java在页面之间导航,java,selenium,Java,Selenium,我浏览一个链接列表,一个接一个地点击它们,我进入链接页面,了解需要执行的操作,然后返回列表,点击下一个链接,它工作得非常好 我现在需要的是到达链接的末尾,在那里循环结束,selenium单击“前进”按钮转到下一页,然后再次完成此页的链接计数并再次开始循环 我不能让selenium单击移动,因为它说click()不能在webelento中使用的命令 类型列表click()方法未定义 这是HTML结构: <div id="results-pagination"> <h2 id=

我浏览一个链接列表,一个接一个地点击它们,我进入链接页面,了解需要执行的操作,然后返回列表,点击下一个链接,它工作得非常好

我现在需要的是到达链接的末尾,在那里循环结束,selenium单击“前进”按钮转到下一页,然后再次完成此页的链接计数并再次开始循环

我不能让selenium单击移动,因为它说
click()不能在webelento中使用的命令

类型列表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();
List numberpage=driver.findElements(By.className(“页面链接”);
System.out.println(“numberpage:+numerospaginas.size());
List links=driver.findElements(By.linkText(“to connect”);
System.out.println(“连接计数:+links.size());
《睡眠》(2000年);

对于(int i=0;i它应该是
driver.findElement(By.linkText(“next”))。单击();
driver.findElements
返回
列表
,而
driver.findElement
返回单个
WebElement

此外,该按钮似乎没有“下一步”文本。请尝试按类查找

driver.findElement(By.className("next")).click();
next
文本如下所示

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


结束标记之前使用
next

它应该是
driver.findElement(By.linkText(“next”)。单击();
driver.findElements
返回
列表
,而
driver.findElement
返回单个
WebElement

此外,该按钮似乎没有“下一步”文本。请尝试按类查找

driver.findElement(By.className("next")).click();
next
文本如下所示

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


结束标记之前使用
next

您的单击功能不起作用,因为driver.findElements(By.linkText(“next”)返回一个列表
list
,并且不能对列表对象调用click()

您可以调用click方法来遍历列表:

List<WebElement> WebElementList = driver.findElements(By.linkText("next")); 
        for(WebElement element : WebElementList){
            element.click(); // click can be called on object of WebElement
        }
List WebElementList=driver.findElements(By.linkText(“下一步”);
for(WebElement:WebElementList){
element.click();//可以对WebElement的对象调用click
}

您的单击函数无法使用,因为driver.findElements(By.linkText(“next”))返回一个列表
列表
,并且不能对列表对象调用click()

您可以调用click方法来遍历列表:

List<WebElement> WebElementList = driver.findElements(By.linkText("next")); 
        for(WebElement element : WebElementList){
            element.click(); // click can be called on object of WebElement
        }
List WebElementList=driver.findElements(By.linkText(“下一步”);
for(WebElement:WebElementList){
element.click();//可以对WebElement的对象调用click
}

我修改了代码,在谷歌搜索结果页面中进行迭代,并获取结果的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"));
    }
}
publicstaticvoidsearchgoogle(字符串查询)抛出InterruptedException{
试一试{
System.setProperty(“webdriver.chrome.driver”、“chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“http://www.google.co.uk");
WebElement=driver.findElement(By.name(“q”));
element.sendKeys(“\”+query+“\”文件类型:pdf\n”);
元素。提交();
//等待谷歌页面显示结果
WebElement myDynamicElement=(新的WebDriverWait(驱动程序,10))
。直到(预期条件。元素的存在(按.id(“结果状态”));
获取结果(驱动程序);
睡眠(1000);
对于(int i=0;i<10;i++){
driver.findElement(By.linkText(“下一步”))。单击();
睡眠(1000);
获取结果(驱动程序);
}
}捕获(例外e){
System.err.println(“错误捕获-”+e);
}
}
公共静态void getResults(WebDriver驱动程序){
列表findElements=null;
findElements=driver.findElements(By.xpath(“//*[@id='rso']///h3/a”);
for(WebElement WebElement:findElements){
System.out.println(webElement.getAttribute(“href”);
}
}

我修改了代码,在谷歌搜索结果页面中进行迭代,并获取结果的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"));
    }
}
publicstaticvoidsearchgoogle(字符串查询)抛出InterruptedException{
试一试{
System.setProperty(“webdriver.chrome.driver”、“chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“http://www.google.co.uk");
WebElement=driver.findElement(By.name(“q”));
element.sendKeys(“\”+query+“\”文件类型:pdf\n”);
元素。提交();
//等待谷歌页面显示结果
WebElement myDynamicElement=(新的WebDriverWait(驱动程序,10))
。直到(预期条件。元素的存在(按.id(“结果状态”));
获取结果(驱动程序);
睡眠(1000);
对于(int i=0;i<10;i++){
driver.findElement(By.linkText(“下一步”))。单击();
睡眠(1000);
获取结果(驱动程序);
}
}捕获(例外e){
System.err.println(“错误捕获-”+e);
}
}
公共静态void getResults(WebDriver驱动程序){
列表findElements=null;
findElements=driver.findElements(By.xpath(“//*[@id='rso']///h3/a”);
for(WebElement WebElement:findElements){
System.out.println(webElement.getAttribute(“href”);
}
}

我已经尝试过这种方法,但不起作用,它无法识别click();命令。该类型的方法click()未定义List@PauloSilva注意
driver.findElement
。不带
s的拼写