Selenium和Geckodriver在Python中创建WebDriver的问题 [英] Selenium and Geckodriver issue with creating a webdriver in Python

查看:103
本文介绍了Selenium和Geckodriver在Python中创建WebDriver的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在曾经工作的python搜寻器中有一段代码.我将其安装在新系统上,现在尝试获取正确的依赖项.使用geckodriver 0.13.0并执行以下代码时:

I have a piece of code in a python crawler that used to work. I installed it on a new system, and am trying to get the right dependencies now. When using geckodriver 0.13.0 and executing the following code:

        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 + "")

我收到以下错误:

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

我在一些地方读到geckodriver有一个bug,我应该使用0.16.0.所以我已经尝试过0.17.0了,但是现在出现了以下错误:

I read in a few places that geckodriver has a bug with this, and I should use 0.16.0. So I've tried that as well as 0.17.0, but am now getting the following error:

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

就好像我现在甚至无法初始化驱动程序一样.我正在使用Selenium 3.4.3,从我的阅读中来看,这很好.

It's as if I now can't even initialize the driver. I am using Selenium 3.4.3, which from what I read is fine.

如果有人可以指导我寻求解决方案,我将不胜感激!谢谢

If anyone can guide me towards a solution, I’d appreciate it much! Thanks

推荐答案

是的,您有两个不同的问题.

You are right, you have two different issues.

问题与geckodriver 0.13.0:

这很可能是因为您的cundefined.

This is most likely because your c is undefined.

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

You have to verify/assert that self.config.email actually returns a valid string (email). So do a check that c contains your expected email before issuing the .send_keys() command.

这里值得一提的另一项增强功能是在查找用户名字段时缺乏安全性.您应该使用明确等待

Another enhancement worth noting here is your lack of safety when it comes to finding the username field. You should use an explicit wait !

# 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!"

最后,添加完整的代码段,因为您似乎在循环浏览电子邮件列表并在先前找到的用户名字段中发送它们.

Lastly, add the full snippet because it looks like you're cycling through a list of emails and sending them in the previously found username field.

问题与geckodriver 0.16.0:

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

This is failing because you have an issue with your driver instantiation: self.driver = webdriver.Firefox().

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

Please update the question with the complete snippet of your driver declaration (including capabilities & profiles, if any). Else, it's really hard to figure out the cause of the error.

这篇关于Selenium和Geckodriver在Python中创建WebDriver的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆