[英]How to solve the org.openqa.selenium.NoSuchElementException
我正在嘗試運行此程序:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HtmlDriver {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.stumbleupon.com/home/");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getPageSource());
}
}
我得到以下例外:
線程“main”中的異常org.openqa.selenium.NoSuchElementException:無法找到名稱為的元素:q系統信息:os.name:'Linux',os.arch:'i386',os.version:'2.6.27- 7-generic',java.version:'1.6.0_12'驅動程序信息:driver.version:HtmlDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:651)org.openqa.selenium.By $ 4 .findElement(By.java:148)org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call(HtmlUnitDriver.java:1133)org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call(HtmlUnitDriver.java:1)at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:869)org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1130)org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement( HtmlUnitDriver.java:330)在com.webdrivertest.HtmlDriver.main(HtmlDriver.java:20)
請幫我解決一下。
該頁面上沒有name =“q”的元素,因此,NoSuchElementException。 您從谷歌獲取示例並更改了它所訪問的網站,但它仍然在頁面上尋找谷歌搜索框。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
try {
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
將HtmlUnitDriver更改為FirefoxDriver時,它是可行的!
使用Selenium站點中的示例,就像它一樣,測試失敗並且具有相同的NoSuchElementException
。 當使用瀏覽器模擬(例如BrowserVersion.FIREFOX_3
實例化時,它也會失敗。
HtmlUnitDriver是否可以工作? 是否需要先以某種方式配置它?
更新:我坐在代理后面,與使用真實瀏覽器的驅動程序不同,此驅動程序不知道代理。 必須在測試用例中手動配置它,並調用:
HtmlUnitDriver.setProxy(host, port);
我還沒有想出如何使用用戶名和密碼為需要身份驗證的代理配置它。
嘗試定義WebDriver
以明確使用Firefox:
WebDriver driver = new FirefoxDriver();
我們不能使用Normal WebElement
For Submit
相反,你可以嘗試
WebElement form = driver.findElement(By.id("formid")); //Id of FORM Tag
form.submit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.