send_keys()使用firefox在python 3 selenium代码中给出错误

时间:2017-05-28 05:04:11

标签: python-3.x selenium firefox selenium-webdriver

我的代码是:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

baseurl = "https://www.google.ca/?gfe_rd=cr&ei=J5ooWerXOsf_8AebtKKICw&gws_rd=ssl"
search = "panda"

xpaths = { 'searchbox' : ".//*[@id='lst-ib']",
   'submit' : ".//*[@id='tsf']/div[2]/div[3]/center/input[1]",
   'img' : ".//*[@id='gbw']/div/div/div[1]/div[2]/a"
 }

driver = webdriver.Firefox()
driver.get(baseurl)

driver.find_element_by_xpath(xpaths['searchbox']).clear()
driver.find_element_by_xpath(xpaths['searchbox']).send_keys(search)
driver.find_element_by_xpath(xpaths['submit']).click()
#driver.find_element_by_xpath(xpaths['img']).click()

Firefox打开,但什么也没发生,并在终端中写入如下:

Traceback (most recent call last):
File "sg1.py", line 21, in <module>
driver.find_element_by_xpath(xpaths['searchbox']).send_keys(search)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected [object Undefined] undefined to be a string

注意事项: Firefox是最新的。

当Firefox打开时,它会打开一个纯文本版本,而不是通常用adblocker,firebug等附加组件打开的版本。

当我只是点击'img'位时,它被注释掉了,它也做了它应该做的事情。

1 个答案:

答案 0 :(得分:1)

此问题与geckodriver v.015相同;为了解决此问题,您的geckodriver版本也会更新到0.16 selenium到3.4.0。