标签云

微信群

扫码加入我们

WeChat QR Code

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?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 pagesint 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 pagedriver.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.0Listening on port 371011010Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 0at java.util.ArrayList.rangeCheck(Unknown Source)at java.util.ArrayList.get(Unknown Source)at Search.GoogleSearch.main(GoogleSearch.java:48)


Why do you want to reload the google search results multiple times? Load it only once, you have the result that you can process further. I would use JSoup for page analysis instead of Selenium.

2019年04月19日24分13秒

tak3shiIn fact , I 'm learning selenium and so creating some code as an example. desire has not utilizaqr the jsoup , just want to be able to navigate through the URLs and then move on to the next search pages and click on the next link , that's all .

1970年01月01日00分03秒

ok, thats fine, but google will detect the automated access and at some point you will receive a captcha page instead of the search results.

2019年04月19日24分13秒

Did this exception is in other drivers (firefox, chromedriver) ??

2019年04月19日24分13秒

SkorpEN IE11, Firefox e Chrome

2019年04月20日24分13秒

He correctly navigated to some extent, but on the third page stood in one of the accessed links and did not return to the list of search , you tell me how to solve it , to keep it going through the pages correctly?The variable pagesearch {String pagesearch = driver.getCurrentUrl();} records url of the search, but I think she is recording only the initial search URL and not the following page , can it be?

2019年04月19日24分13秒

Shows no error in the execution and not on the island , is just caught in the accessed link.

2019年04月20日24分13秒

PauloSilva You have to options, capture the url in the external loop (added to my answer) or use driver.navigate().back(); instead of driver.navigate().to(pagesearch);

2019年04月19日24分13秒

I used his new example with driver.navigate().to( pagesearch); and for some reason he goes to the second page , click 01 link and then returns to the page 1 and click again on all already links clicked before, strange, would have some way of treating this behavior, so it click all the links on page 1 , scroll to page 2 and do the same , sme repeat the links?

2019年04月20日24分13秒

PauloSilva Put the String pagesearch = driver.getCurrentUrl(); after if (j > 1) condition, that way it will move to next page before taking the url.

2019年04月19日24分13秒