How do I use the HtmlUnit driver with Selenium through the Python bindings?

后端 未结 4 1208
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 08:41

I\'m using WebDriver through the Python bindings located on Google\'s site. According to the documentation here, it supports four browsers: Chrome, IE, Firefox, and HtmlUnit.

相关标签:
4条回答
  • 2021-02-01 08:52

    I use it like this:

    from selenium.remote import connect                                                                                                                          
    
    b = connect('htmlunit')                                                                                                                                      
    b.get('http://google.com')                                                                                                                                   
    
    q = b.find_element_by_name('q')                                                                                                                              
    q.send_keys('selenium')                                                                                                                                      
    q.submit()                                                                                                                                                   
    
    for l in b.find_elements_by_xpath('//h3/a'):                                                                                                                 
        print('%s\n\t%s\n' % (l.get_text(), l.get_attribute('href')))
    
    0 讨论(0)
  • 2021-02-01 08:58

    HtmlUnit is a Java library so the only choice for non-java WebDriver bindings is to use a RemoteWebDriver. You will need to start a Selenium Server and connect to it specifying the HtmlUnit as desired browser.

    I am not very familiar with Python, but according to http://code.google.com/p/selenium/wiki/PythonBindings it should look something like:

    from selenium.remote import connect
    from selenium import HTMLUNIT
    
    
    wd = connect(HTMLUNIT, server="http://<selenium_server>:4444")
    
    0 讨论(0)
  • 2021-02-01 09:05

    I found the answer at https://stackoverflow.com/a/5518175/125170

    As of the 2.0b3 release of the python client you can create an HTMLUnit webdriver via a remote connection like so:

    from selenium import webdriver
    driver = webdriver.Remote(
      desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
    driver.get('http://www.google.com')
    

    You can also use the HTMLUNITWITHJS capability item for a browser with Javascript support.

    Note that you need to run the Selenium Java server for this to work, since HTMLUnit is implemented on the Java side.

    0 讨论(0)
  • 2021-02-01 09:06

    // You can use HtmlUnitDriver in this case.

           import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    

    // Declaring and initialising the HtmlUnitWebDriver

        HtmlUnitDriver unitDriver = new HtmlUnitDriver();
    

    // open google.com webpage

        unitDriver.get("http://google.com");
    
    0 讨论(0)
提交回复
热议问题