无法在 Selenium 中定位元素 (htmlUnitDriver)
Unable to locate an element in Selenium (htmlUnitDriver)
我无法在我使用的 selenium 中找到一个元素 htmlUnitDriver
。驱动程序运行良好,但我无法找到 google 搜索文本框元素。
代码如下:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class SampleUnitDriver
{
public static void main(String[] args) throws Exception
{
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
unitDriver.get("http://google.com");
System.out.println("Title of the page is -> " + unitDriver.getTitle());
WebElement searchBox = unitDriver.findElement(By.xpath(".//*[@id='gs_htif0']"));
searchBox.sendKeys("Selenium");
WebElement button = unitDriver.findElement(By.name("gbqfba"));
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());
}
}
这是一个错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate a node using .//*[@id='gs_htif0']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15
10:43:46'
System info: host: 'user-PC', ip: '192.168.1.52', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:
'1.8.0_51'
Driver info: driver.version: SampleUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1165)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.call(HtmlUnitDriver.java:1725)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.call(HtmlUnitDriver.java:1721)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
at com.digitalmqc.automation.action.SampleUnitDriver.main(SampleUnitDriver.java:19)
如有任何帮助,我们将不胜感激。
在查找元素之前添加一些显式等待:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='gs_htif0']"))));
searchBox.sendKeys("Selenium");
您正在定位错误的元素,您应该尝试如下:-
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
unitDriver.get("http://google.com");
System.out.println("Title of the page is -> " + unitDriver.getTitle());
WebElement searchBox = unitDriver.findElement(By.name("q"))
searchBox.sendKeys("Selenium");
WebElement button = unitDriver.findElement(By.name("btnG"));
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());
希望对您有所帮助..:)
我也遇到了同样的问题。但在添加代理详细信息后已解决。
您可以通过在调用 get 方法后添加以下代码来验证您是否面临同样的问题
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getPageSource());
你会看到
http://www.google.com/
Unknown host
因此您需要将代理添加到您的代码中
WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);
Proxy proxy = new Proxy();
proxy.setHttpProxy("XXX.XX.XX.XX:8080");
((HtmlUnitDriver) driver).setProxySettings(proxy);
希望这对任何人都有帮助。肯定会节省几个小时的搜索时间
我无法在我使用的 selenium 中找到一个元素 htmlUnitDriver
。驱动程序运行良好,但我无法找到 google 搜索文本框元素。
代码如下:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class SampleUnitDriver
{
public static void main(String[] args) throws Exception
{
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
unitDriver.get("http://google.com");
System.out.println("Title of the page is -> " + unitDriver.getTitle());
WebElement searchBox = unitDriver.findElement(By.xpath(".//*[@id='gs_htif0']"));
searchBox.sendKeys("Selenium");
WebElement button = unitDriver.findElement(By.name("gbqfba"));
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());
}
}
这是一个错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//*[@id='gs_htif0'] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46' System info: host: 'user-PC', ip: '192.168.1.52', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51' Driver info: driver.version: SampleUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1165) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.htmlunit.HtmlUnitDriver.call(HtmlUnitDriver.java:1725) at org.openqa.selenium.htmlunit.HtmlUnitDriver.call(HtmlUnitDriver.java:1721) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606) at com.digitalmqc.automation.action.SampleUnitDriver.main(SampleUnitDriver.java:19)
如有任何帮助,我们将不胜感激。
在查找元素之前添加一些显式等待:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='gs_htif0']"))));
searchBox.sendKeys("Selenium");
您正在定位错误的元素,您应该尝试如下:-
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
unitDriver.get("http://google.com");
System.out.println("Title of the page is -> " + unitDriver.getTitle());
WebElement searchBox = unitDriver.findElement(By.name("q"))
searchBox.sendKeys("Selenium");
WebElement button = unitDriver.findElement(By.name("btnG"));
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());
希望对您有所帮助..:)
我也遇到了同样的问题。但在添加代理详细信息后已解决。 您可以通过在调用 get 方法后添加以下代码来验证您是否面临同样的问题
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getPageSource());
你会看到
http://www.google.com/
Unknown host
因此您需要将代理添加到您的代码中
WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);
Proxy proxy = new Proxy();
proxy.setHttpProxy("XXX.XX.XX.XX:8080");
((HtmlUnitDriver) driver).setProxySettings(proxy);
希望这对任何人都有帮助。肯定会节省几个小时的搜索时间