How do I use the HTMLUnit driver with Selenium from Python?

前端 未结 3 1303
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 03:46

How do I tell Selenium to use HTMLUnit?

I\'m running selenium-server-standalone-2.0b1.jar as a Selenium server in the background, and the latest Python bindings inst

相关标签:
3条回答
  • 2020-12-14 04:15

    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)
  • 2020-12-14 04:18

    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)
  • 2020-12-14 04:30

    using the selenium 2.20.0.jar server and matching python version, I am able to use HtmlUnitDriver by specifying the browser as *mock

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    server_url = "http://%s:%s/wd/hub" % (test_host, test_port)
    dc = DesiredCapabilities.HTMLUNIT
    wd = webdriver.Remote(server_url, dc)
    wd.get('http://www.google.com')
    
    0 讨论(0)
提交回复
热议问题