2014-08-30 85 views
1

我把从硒IDE导出选项此Python代码,我取代self.driver = webdriver.Firefox()与HtmlUnitDriver(),但它不工作,因为我有一个错误:如何使用Python绑定在Selenium webdriver中加载HtmlUnit驱动程序?

AttributeError: 'module' object has no attribute 'HtmlUnitDriver' 

的代码是:

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import Select 
from selenium.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import NoAlertPresentException 
import unittest, time, re 

class Scrap(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.HtmlUnitDriver() 
     self.driver.setJavascriptEnabled(true) 
     self.driver.implicitly_wait(30) 
     self.base_url = "http://www.google.com/" 
     self.verificationErrors = [] 
     self.accept_next_alert = True 

    def test_scrap(self): 
     driver = self.driver 
.... 

你能指出我在做什么错吗?我想使用HtmlUnit而不是Firefox来查看我是否可以用更少的资源更快地实现我的目标。如果可能,我需要激活Javascript。如果可能的话,您是否也可以告诉我我应该从文档中获取这些信息的位置,因为我找不到它?

+0

可能重复[我如何使用与化的HtmlUnit司机硒通过Python绑定?](http://stackoverflow.com/questions/4081724/how-do-i-use-the-htmlunit-driver-with-selenium-through-the-python-bindings) – Beetle 2014-12-29 12:50:44

回答

4

不确定,但AFAIK HtmlUnitDriver仅在您使用webdriver的java版本 时可用。如果你想可以使用HtmlUnit,司机在Python中,你需要启动独立服务器

java -jar selenium-server-standalone-x.x.x.jar 

,然后连接到通过远程服务器使用

from selenium import webdriver 
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNIT) 
//or with enabled js 
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS) 
相关问题