0

我无法在我使用的硒中找到一个元素htmlUnitDriver。很好的驱动程序工作正常,但我无法找到谷歌搜索文本框元素。

这是代码:

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


    }
}

这是一个错误:

线程“主”org.openqa.selenium.NoSuchElementException 中的异常:无法使用 .//*[@id='gs_htif0'] 定位节点有关此错误的文档,请访问:http ://seleniumhq.org/exceptions /no_such_element.html 构建信息:版本:'2.53.0',修订:'35ae25b1534ae328c771e0856c93e187490ca824',时间:'2016-03-15 10:43:46' 系统信息:主机:'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.version: SampleUnitDriver at org.openqa.selenium.htmlunit .HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1165) 在 org.openqa.selenium.By$ByXPath.findElement(By.java:361) 在 org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1725)在 org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367) 在 org.openqa 的 org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1721)。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)

任何帮助都将不胜感激。

4

3 回答 3

1

您正在定位错误的元素,您应该尝试如下:-

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

希望能帮助到你..:)

于 2016-07-15T12:36:10.820 回答
0

在查找元素之前添加一些显式等待:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='gs_htif0']"))));
searchBox.sendKeys("Selenium");
于 2016-07-15T12:10:56.517 回答
0

我也面临同样的问题。但在添加代理详细信息后解决。您可以通过在 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);

希望这会帮助任何人。肯定会节省几个小时的搜索时间

于 2018-08-24T09:46:51.557 回答