检查5页中的500个标题并检查每个标题的标题: 请告诉我更正
WebElement value = driver.findElement(By.id("numberOfResults"));
Select selectElement = new Select(value);
selectElement.selectByValue("100");
List<WebElement> PostTitles = driver.findElements(By.className("hlFld-Title"));
for (WebElement eachTitle : PostTitles)
{
System.out.println(eachTitle.getText());
// String Titles = eachTitle.getText();
int Size = PostTitles.size();
for (int i = 0; i < eachTitle; i++)
{
driver.findElement(By.linkText(eachTitle.getText())).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.linkText("HEADING")).click();
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
driver.navigate().back();
}
}
答案 0 :(得分:0)
很难说出你的情景究竟是什么,但我认为这会奏效。
一些修正:
.implicitlyWait()
一次,它就适用于WebDriver
的生命。eachTitle
。
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // this only needs to be set once
WebElement value = driver.findElement(By.id("numberOfResults"));
Select selectElement = new Select(value);
selectElement.selectByValue("100");
List<WebElement> PostTitles = driver.findElements(By.className("hlFld-Title"));
for (WebElement eachTitle : PostTitles)
{
String title = eachTitle.getText();
System.out.println(title);
driver.findElement(By.linkText(title)).click();
driver.findElement(By.linkText("HEADING")).click();
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
driver.navigate().back();
}
答案 1 :(得分:0)
for (int j = 0; j < 6; j++)
{
WebElement value = driver.findElement(By.id("numberOfResults"));
Select selectElement = new Select(value);
selectElement.selectByValue("100");
List<WebElement> postTitles = driver.findElements(By.className("hlFld-Title"));
for(int i = 0; i < postTitles.size(); i++) {
system.out.println(postTitles.get(i).getText());
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.linkText(postTitles.get(i).getText())).click();
driver.findElement(By.linkText("HEADING")).click();
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
driver.navigate().back();
}
答案 2 :(得分:0)
实施此逻辑,成功执行,无错误
List<WebElement> linkElements=driver.findElements(By.xpath("************"));
String[] linkTitles = new String[linkElements.size()];
int i = 0;
//extract the link texts of each link element
for (WebElement e : linkElements)
{
linkTitles[i] = e.getText();
i++;
}
//Test each link
for (String t : linkTitles)
{
// Titles Click
driver.findElement(By.linkText(t)).click();
System.out.println("\n"+ driver.getTitle());
Thread.sleep(7000);
driver.findElement(By.linkText("Heading Notes")).click();
System.out.println(driver.findElement(By.xpath("*****")).getText());
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
driver.navigate().back();
Thread.sleep(7000);
}