我走了一个链接列表,我逐个点击它们,我转到页面链接并实现需要执行的操作然后返回列表点击下一个链接,它工作正常
我现在需要的是到达链接的末尾,循环结束,selenium单击前进按钮转到下一页并再次完成此页面的链接计数并再次开始循环。
我不能让selenium点击移动,因为它说click();
命令你不能在webelento中使用。
方法click()未定义类型List< WebElement>
这是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">< 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>
selenium代码:
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();
答案 0 :(得分:3)
您的点击功能未到来,因为 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
}
答案 1 :(得分: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>
。
答案 2 :(得分:1)
我修改了代码以遍历Google搜索结果页并获取结果的网址。
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"));
}
}