2013-06-06 4 views
3

Je n'arrive pas à obtenir HtmlUnitDriver pour trouver des éléments sur une page à laquelle j'essaie d'accéder. WebDriver d'autre part fonctionne bien. Les deux utilisent la même méthode. Voici mon code:HtmlUnitDriver est incapable de localiser un nœud

import java.util.logging.*; 
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; 
import com.gargoylesoftware.htmlunit.BrowserVersion; 


public class Main { 


    public static void main(String[]args) 
    { 
     FirefoxDriver driver2=new FirefoxDriver(); 

     HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_10); 
     driver.setJavascriptEnabled(true); 
     Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

     runTest(driver2); //works 
     runTest(driver); //does not work 
    } 

    public static void runTest(WebDriver driver) 
    { 
     driver.get("http://10.3.1.164"); 
     WebElement elem=driver.findElement(By.xpath("//td[2]/input")); 
     assert elem.isDisplayed(); 
     System.out.println(driver.getTitle()); 
    } 
} 

Voici le stacktrace:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to  locate a node using //td[2]/input 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45' 
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_10' 
Driver info: driver.version: HtmlUnitDriver 
at  org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:805) 
at org.openqa.selenium.By$ByXPath.findElement(By.java:344) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1247) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:987) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1244) 
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:393) 
at Main.runTest(Main.java:28) 
at Main.main(Main.java:22) 
+0

Et qu'est-ce que le code HTML de l'apparence de la page comme? (Est-il possible que les éléments soient dans un espace de noms, comme XHTML?) Est-ce que la page nécessite l'exécution de javascript pour générer du code HTML? – LarsH

+0

Je ne suis pas autorisé à publier du code html mais en regardant de plus près, il semble que le code html soit généré dynamiquement par javascript. Je sais cela parce que quand je fais un clic droit, voir la source de la page (dans Firefox) non des tags que je cherche apparaissent. –

Répondre

5

I figured it out. C'était juste une question de moi d'attendre que les éléments soient chargés. Voici le code.

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.util.logging.*; 
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.*; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import com.gargoylesoftware.htmlunit.BrowserVersion; 
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 

public class Main { 


public static void main(String[]args) throws FailingHttpStatusCodeException, MalformedURLException, IOException 
{ 
    FirefoxDriver driver2=new FirefoxDriver(); 

    HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6); 

    driver.setJavascriptEnabled(true); 
    Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

    runTest(driver2); //works 
    runTest(driver); //does not work 
} 

public static void runTest(WebDriver driver) 
{ 
    driver.get("http://10.3.1.164"); 
    WebElement elem=(new WebDriverWait(driver, 10)) //added this line 
    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//td[2]/input"))); 
    System.out.println(elem.getAttribute("id")); 
    assert elem.isDisplayed(); 
    System.out.println(driver.getTitle()); 
} 
} 
0

Vous avez juste besoin de prendre en charge timeOut. Je viens de rencontrer un simple test HTMLUnitDriver pour les Lates d'aujourd'hui availabel sélénium-server-standalone.jar qui est 3.8.1

Voici mon code java:

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver;  
public class HD_HTMLUnitTest {    
     public static void main(String[] args) throws InterruptedException { 
        // Creating a new instance of the HTML unit driver 
      //System.getProperties().put("http.proxyHost", "www-proxy.us.oracle.com"); 
      //System.getProperties().put("http.proxyPort", "80"); 

      HtmlUnitDriver unitDriver = new HtmlUnitDriver(); 
      //unitDriver.setJavascriptEnabled(true); 

      unitDriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
      unitDriver.get("https://www.google.co.in"); 
      System.out.println("Title of the page is -> " + unitDriver.getTitle()); 

      unitDriver.findElement(By.name("q")).sendKeys("HTMLUnit through Selenium"); 
      //searchBox.sendKeys("Selenium"); 
      WebElement button = unitDriver.findElement(By.name("btnK")); 
      button.click(); 
      System.out.println("Title of the page is -> " + unitDriver.getTitle());  

      unitDriver.quit(); 
     }  
} 
Questions connexes