2016-11-16 83 views
4

我正在学习WebDriver,并试图检查demoaut网站上的链接。循环中的代码应该通过其标题识别“Under Construction”页面,打印出第一行,然后返回到基础URL。但是这不会因为某些原因而发生。第一个“正在建设中”的链接(特色度假目的地)没有被识别,提示错误的行被打印,然后由于NoSuchElementException而返回崩溃,因为它正在寻找错误的链接页。这是为什么发生?为什么它根据“正在建设中”页面的标题行事?WebDriver打印错误的条件语句

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

} 

回答

1

,在linkTexts所有的引用都将变得陈旧......即使如果你回到页面。你需要做的是将所有的hrefs存储在List中,然后导航到每个href并检查页面的标题。

我就这样写吧...

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

您的代码在我的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. 
0

我没有发现任何错误在你logic.Infact我复制你的代码,刚刚更换驾驶员的Firefox与IE的驱动程序和它的工作作为expected.Below是我在运行代码时获得的控制台输出:

> 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.