2017-09-15 39 views
0

我是一名初學者,我正在做一個測試,但我不明白爲什麼它不起作用。 這是我的代碼和下面的錯誤。使用硒HTMLUnitDriver簡單測試不能正常工作,是否錯誤?

import org.openqa.selenium.*; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 
public class PruebaHTMLUnit { 
    public static void main(String[] args) { 

WebDriver driver = new HtmlUnitDriver(); 
driver.get("http://www.google.com"); 
WebElement element = driver.findElement(By.name("q")); 
element.sendKeys("Guru99"); 
element.submit(); 
System.out.println("Page title is: " + driver.getTitle()); 

driver.quit(); 
} 
} 

和錯誤:

Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z' 
System info: host: 'MVILLEGAS764', ip: '169.254.106.219', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: 

'1.8.0_144' Driver info: driver.version: HtmlUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1293) at org.openqa.selenium.By$ByName.findElement(By.java:303) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1970) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1606) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1966) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:754) at com.villegas.pruebas.PruebasRegresion.PruebaHTMLUnit.main(PruebaHTMLUnit.java:20)

+0

添加到您的問題,說清楚什麼行不通,它應該如何工作,爲代碼添加適當的降價格式。 – svgrafov

+0

你正在使用什麼罐子 – iamsankalp89

+0

我怎麼知道我使用的罐子? – mvillegas

回答

0
import org.openqa.selenium.*; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

    public class test_Stackoverflow 
    { 
      public static void main(String[] args) { 
         WebDriver driver = new HtmlUnitDriver(); 
       driver.get("https://www.google.com"); 
       WebElement element = driver.findElement(By.xpath(".//*[@id='gs_htif0']")); 
       element.sendKeys("Guru99"); 
       element.submit(); 
       System.out.println("Page title is: " + driver.getTitle()); 
       driver.quit(); 
      } 
    } 

HtmlUnitDriver是硒2.53版本的一部分。如果您使用的是Selenium 2.52或更早版本,則無需下載並安裝HtmlUnitDriver,它已經在那裏。

但你仍然想使用它從這裏

https://github.com/SeleniumHQ/htmlunit-driver/releases

下載它從2.53版本,你需要添加它分別

看到該圖片Image of result

試試這個:

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver --> 
<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-htmlunit-driver</artifactId> 
    <version>2.52.0</version> 
</dependency> 
+0

我怎麼知道我使用的是哪種硒?我thnk <! - https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java - > org.seleniumhq.selenium 硒的Java 3.5.1 mvillegas

+0

右鍵單擊項目 - >去建立路徑 - >添加外部罐子 – iamsankalp89

+0

您正在使用3.5.1 – iamsankalp89

0

Ple ASE試試這個希望它會工作

import org.openqa.selenium.*; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 
    /** 
    * Created by W3E64 on 9/15/2017. 
    */ 
    public class testing_solution 
    { 
public static void main(String[] args) { 
       java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); 
       java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 
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

威力它的工作........... – iamsankalp89

+0

不工作..... – mvillegas

+0

可以請你分享你目前的錯誤? – zsbappa

相關問題