简体   繁体   English

在带有硒的Google搜索页面之间导航-Java

[英]navigate between pages Google Search with selenium - Java

How do I search for something on Google , click on the link , then return to the search and click the following link ( without repeating the already links clicked ) and after the links that the page finished , navigate to the page 2 and repeat the same steps in succession? 如何在Google上搜索内容,单击链接,然后返回搜索并单击以下链接(不重复已单击的链接),在页面链接完成后,导航至页面2并重复相同的操作一步一步?

So far I could go to the first link, go to the page and return to the list of links searched only. 到目前为止,我可以转到第一个链接,转到页面,然后返回到仅搜索的链接列表。

    package Search;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GoogleSearch {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub


             System.setProperty("webdriver.ie.driver", "C:/Users/paulo.roberto/Documents/eclipse/Selenium/IEDriverServer.exe");
              DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
              caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
              WebDriver driver = new InternetExplorerDriver(caps);          
                driver.manage().window().maximize();
                driver.manage().deleteAllCookies();

            driver.get("https://www.google.com.br");

            driver.findElement(By.name("q")).sendKeys("test");
            driver.findElement(By.name("btnG")).click();

            WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats"))); 


            // find the number of pages
            int size = driver.findElements(By.cssSelector("[valign='top'] > td")).size();
            for(int j = 1 ; j < size ; j++) {
                if (j > 1) {// we don't need to navigate to the first page
                    driver.findElement(By.cssSelector("[aria-label='Page " + j + "']")).click(); // navigate to page number j
                }

                String pagesearch = driver.getCurrentUrl();

                List<WebElement> findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a"));
                System.out.println(findElements.size());

                for(int i=0;i<findElements.size();i++){
                    findElements= driver.findElements(By.xpath("//*[@id='rso']//h3/a"));                
                    findElements.get(i).click(); 

                    driver.navigate().to(pagesearch);
                    // or driver.navigate().back();
                }
            }



    }

}

message on the console: 控制台上的消息:

 Started InternetExplorerDriver server (32-bit) 2.48.0.0 Listening on port 37101 10 10 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Search.GoogleSearch.main(GoogleSearch.java:48) 

You can use nested loops. 您可以使用嵌套循环。 One to navigate between results pages and one to click on results 一种浏览结果页面,另一种点击结果

// find the number of pages
int size = driver.findElements(By.cssSelector("[valign='top'] > td")).size();
for(int j = 1 ; j < size ; j++) {
    if (j > 1) {// we don't need to navigate to the first page
        driver.findElement(By.cssSelector("[aria-label='Page " + j + "']")).click(); // navigate to page number j
    }

    String pagesearch = driver.getCurrentUrl();

    List<WebElement> findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a"));
    System.out.println(findElements.size());

    for(int i=0;i<findElements.size();i++){
        findElements= driver.findElements(By.xpath("//*[@id='rso']//h3/a"));                
        findElements.get(i).click(); 

        driver.navigate().to(pagesearch);
        // or driver.navigate().back();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM