【问题标题】:How to load HtmlUnit driver in Selenium webdriver using Python bindings?如何使用 Python 绑定在 Selenium webdriver 中加载 HtmlUnit 驱动程序?
【发布时间】:2014-08-30 07:27:43
【问题描述】:

我从 Selenium IDE 导出选项中提取了这个 Python 代码,并将 self.driver = webdriver.Firefox() 替换为 HtmlUnitDriver() 但它不起作用,因为我有一个错误:

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 Scrape(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_scrape(self):
        driver = self.driver
....

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

【问题讨论】:

标签: python selenium selenium-webdriver webdriver web-scraping


【解决方案1】:

不确定,但仅当您使用 java 版本时,AFAIK HtmlUnitDriver 才可用 的网络驱动程序。如果你想在 python 中使用 htmlunit 驱动,你需要启动独立服务器

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) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2018-10-30
    相关资源
    最近更新 更多