无法在Selenium中找到元素(htmlUnitDriver)

时间:2016-07-15 12:05:54

标签: selenium selenium-webdriver htmlunit-driver

我无法在selenium中找到一个元素,我使用了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());


    }
}

以下是错误:

  

线程“main”中的异常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         在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 $ 5.call(HtmlUnitDriver.java:1721)         在org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367)         在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721)         在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)         在com.digitalmqc.automation.action.SampleUnitDriver.main(SampleUnitDriver.java:19)

任何帮助都可以表示赞赏。

3 个答案:

答案 0 :(得分: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());

希望它有所帮助.. :)

答案 1 :(得分:0)

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

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='gs_htif0']"))));
searchBox.sendKeys("Selenium");

答案 2 :(得分: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);

希望这对任何人都有帮助。当然可以节省几个小时的搜索

相关问题