【问题标题】:WebDriver prints wrong conditional statementWebDriver 打印错误的条件语句
【发布时间】:2017-03-31 09:09:54
【问题描述】:

我正在学习 WebDriver,只是想查看 demoaut 网站上的链接。循环中的代码应该通过标题识别“建设中”页面,打印出第一行,然后返回基本 url。但由于某种原因,这不会发生。它到达的第一个“正在建设”链接(特色度假目的地)未被识别,提示打印错误的行,然后由于 NoSuchElementException 而不是返回它崩溃,因为它正在寻找错误的链接页。为什么会这样?为什么不根据“建设中”页面的标题进行操作?

import java.util.List;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CheckLinks {

public static void main(String[] args) {
    String baseUrl = "http://newtours.demoaut.com/";
    System.setProperty("webdriver.gecko.driver", "C:\\Workspace_e\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    String underConsTitle = "Under Construction: Mercury Tours";
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    driver.get(baseUrl);
    List<WebElement> linkElements = driver.findElements(By.tagName("a"));
    String[] linkTexts = new String[linkElements.size()];
    int i = 0;

    //extract the link texts of each link element
    for (WebElement e : linkElements) {
        linkTexts[i] = e.getText();
        i++;
    }

    //test each link
    for (String t : linkTexts) {
        driver.findElement(By.linkText(t)).click();
        if (driver.getTitle().equals(underConsTitle)) {
            System.out.println("\"" + t + "\""
                    + " is under construction.");
        } else {
            System.out.println("\"" + t + "\""
                    + " is working.");
        }
        driver.navigate().back();
    }
    driver.quit();
}

}

【问题讨论】:

    标签: java eclipse selenium testing selenium-webdriver


    【解决方案1】:

    您的代码在我的 Chrome 浏览器中运行良好。您的问题可能是 webdriver 的速度。您可以使用 WebDriverWait,它是对特定元素的显式等待。

    试试下面的修改代码

    for (String t : linkTexts) {
            WebDriverWait wait = new WebDriverWait(driver, 60);
            wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText(t))));
            driver.findElement(By.linkText(t)).click();
            if (driver.getTitle().equals(underConsTitle)) {
                System.out.println("\"" + t + "\""
                        + " is under construction.");
            } else {
                System.out.println("\"" + t + "\""
                        + " is working.");
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {         
                e1.printStackTrace();
            }
            driver.navigate().back();
        }
    

    我可以通过下面的方式退出

    "Home" is working.
    "Flights" is working.
    "Hotels" is under construction.
    "Car Rentals" is under construction.
    "Cruises" is working.
    "Destinations" is under construction.
    "Vacations" is under construction.
    "SIGN-ON" is working.
    "REGISTER" is working.
    "SUPPORT" is under construction.
    "CONTACT" is under construction.
    "your destination" is under construction.
    "featured vacation destinations" is under construction.
    "Register here" is working.
    "Business Travel @ About.com" is working.
    "Salon Travel" is working.
    

    【讨论】:

      【解决方案2】:

      点击第一个链接后,linkTexts 中的所有引用都将失效……即使您返回该页面。您需要做的是将所有的 href 存储在一个 List 中,然后导航到每一个并检查页面的标题。

      我会这样写...

      public class CheckLinks
      {
          public static void main(String[] args) throws UnsupportedFlavorException, IOException
          {
              String firefoxDriverPath = "C:\\Users\\Jeff\\Desktop\\branches\\Selenium\\lib\\geckodriver-v0.11.1-win32\\geckodriver.exe";
              System.setProperty("webdriver.gecko.driver", firefoxDriverPath);
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
      
              String baseUrl = "http://newtours.demoaut.com/";
              driver.get(baseUrl);
              List<WebElement> links = driver.findElements(By.tagName("a"));
              List<String> hrefs = new ArrayList<>();
              for (WebElement link : links)
              {
                  hrefs.add(link.getAttribute("href"));
              }
              System.out.println(hrefs.size());
              String underConsTitle = "Under Construction: Mercury Tours";
              for (String href : hrefs)
              {
                  driver.get(href);
                  System.out.print("\"" + href + "\"");
                  if (driver.getTitle().equals(underConsTitle))
                  {
                      System.out.println(" is under construction.");
                  }
                  else
                  {
                      System.out.println(" is working.");
                  }
              }
              driver.close();
              driver.quit();
          }
      }
      

      【讨论】:

        【解决方案3】:

        我没有发现你的逻辑有任何问题。事实上我复制了你的代码,只是用 IE 驱动程序替换了 firefox 驱动程序,它按预期工作。下面是我运行代码时得到的控制台输出:

        > Home" is working. "Flights" is working. "Hotels" is under
        > construction. "Car Rentals" is under construction. "Cruises" is
        > working. "Destinations" is under construction. "Vacations" is under
        > construction. "SIGN-ON" is working. "REGISTER" is working. "SUPPORT"
        > is under construction. "CONTACT" is under construction. "your
        > destination" is under construction. "featured vacation destinations"
        > is under construction. "Register here" is working. "Business Travel @
        > About.com" is working.
        

        【讨论】:

          猜你喜欢
          • 2016-08-16
          • 2015-06-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-15
          • 1970-01-01
          • 2013-03-23
          相关资源
          最近更新 更多