2016-03-02 102 views
0

我走了一个链接列表,我一个接一个地点击它们,我去了页面的链接并实现了需要执行的操作,然后返回到列表中点击下一个链接,它的工作完美。在硒页面之间导航 - Java

我现在需要的是到循环结束的地方,硒点击前进按钮进入下一页,再次完成此页面的链接计数并重新开始循环。

我不能让硒点击移动,因为它说click();命令你不能在webelento中使用。

的方法,点击()是未定义的类型列表< WebElement>

list

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

image

回答

1

我修改了代码,通过谷歌搜索结果页面进行迭代,并获得满意的结果的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")); 
    } 
} 
1

它应该是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>结束标记之前。

+0

我已经试过这种方式不起作用,它不能识别click();命令。方法click()未定义类型列表

+0

@PauloSilva注意'driver.findElement'的拼写。没有'''最后。 – Guy

3

您点击功能不来了,因为driver.findElements(By.linkText(“下一步”))返回一个列表List<WebElement>并单击()不能列表对象上调用。

可以调用点击方法我遍历列表:

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