[英]WebDriver prints wrong conditional statement
我正在學習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();
}
}
單擊第一個鏈接后,即使您返回到頁面, linkTexts
所有引用linkTexts
將變為陳舊... 您需要做的是將所有href存儲在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();
}
}
您的代碼在我的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.
我沒有發現你的邏輯有什么問題。事實上我復制了你的代碼,只是用IE驅動程序替換了firefox驅動程序,它按預期工作.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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.