需要检查5个页面中的500个标题(每个页面包含100个标题),每个标题必须单击并检查该页面标题上的特定标题

时间:2016-11-03 10:13:58

标签: java selenium

检查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();
    }
}

3 个答案:

答案 0 :(得分:0)

很难说出你的情景究竟是什么,但我认为这会奏效。

一些修正:

  1. 您只需设置.implicitlyWait()一次,它就适用于WebDriver的生命。
  2. 我认为您的嵌套循环是个问题。您已经在浏览标题...只需使用eachTitle
  3. 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);
}