Selenium和Geckodriver在Python中创建webdriver的问题

Tyr*_*ick 6 python selenium webdriver geckodriver

我在python爬虫中有一段代码,曾经工作过.我将它安装在一个新系统上,我正在努力获得正确的依赖关系.使用geckodriver 0.13.0并执行以下代码时:

        def login(self):
            print self.colors.OKBLUE + "Logging into my site as User: " + self.config.email + self.colors.ENDC
            username = self.driver.find_element_by_css_selector('.my_user_field')
            for c in self.config.email:
                    print "Sending key: " + c
                    username.send_keys(c + "")
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Sending key: b
Traceback (most recent call last):
  File "main.py", line 20, in <module>
    crawler.start()
  File "/home/tyrick/dev/pycrawlers/sc/src/main/python/new.py", line 39, in start
    self.login()
  File "/home/tyrick/dev/pycrawlers/sc/src/main/python/new.py", line 147, in login
username.send_keys(c)
   File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 349, in send_keys
    'value': keys_to_typing(value)})
   File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
   File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected [object Undefined] undefined to be a string
Run Code Online (Sandbox Code Playgroud)

我在几个地方读到geckodriver有这个错误,我应该使用0.16.0.所以我尝试了0.17.0,但现在收到以下错误:

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    crawler = New()
  File "/home/tyrick/dev/pycrawlers/sc/src/main/python/new.py", line 28, in __init__
    self.driver = webdriver.Firefox()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 152, in __init__
keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: elementScrollBehavior was not a the name of a known capability or a valid extension capability
Run Code Online (Sandbox Code Playgroud)

好像我现在甚至无法初始化驱动程序.我正在使用Selenium 3.4.3,从我读到的内容很好.

如果有人能指导我寻求解决方案,我会非常感激!谢谢

iam*_*hiv 5

你是对的,你有两个不同的问题.

问题geckodriver 0.13.0:

这很可能是因为你cundefined.

您必须验证/断言self.config.email实际返回有效字符串(电子邮件).因此,c在发出.send_keys()命令之前,请检查包含您预期的电子邮件.

另一个值得注意的增强功能是在查找用户名字段时缺乏安全性.你应该使用明确的等待!

# Library imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# (...)

def login(self):
    print self.colors.OKBLUE + "Logging into my site as User: " + 
    self.config.email + self.colors.ENDC

    # Polls the DOM for 3 seconds trying to find '.my_user_field'
    username = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.my_user_field')))

    for c in self.config.email:

        # Validate 'c' is of type string
        if (str(type(c)).find('str') != -1):  
            print "Sending key: " + c
            username.send_keys(c + "")
        else:
            print "'c' is not what it used to be!"
Run Code Online (Sandbox Code Playgroud)

最后,添加完整的代码片段,因为它看起来像是在电子邮件列表中循环并在之前找到的用户名字段中发送它们.

问题geckodriver 0.16.0:

这是失败的,因为您的驱动程序实例化存在问题:self.driver = webdriver.Firefox().

请使用驱动程序声明的完整片段(包括功能和配置文件,如果有)更新问题.否则,很难找出错误的原因.