Python 硒:Send_keys 执行失败

Python Selenium: Send_keys Failed to execute

我想用

填写表格
driver.find_element_by_id("name").send_keys("Test")

但我每次 运行 脚本时都会收到此错误:

Traceback (most recent call last):
  File "fill.py", line 22, in <module>
    driver.find_element_by_id("name").send_keys("Test")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 351, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
    'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'.
  (Session info: chrome=66.0.3359.117)
  (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.4 x86_64)

<input first_and_last="true" placeholder="name" class="string required" type="text" name="flname" id="name">

注意:我在搜索名称时也遇到错误。如果我搜索 Xpath,它填写表单的速度非常慢,我需要它更快。

我该如何解决这个问题?

使用任一定位器策略

  • css_selector :

    driver.find_element_by_css_selector("input.string.required#name").send_keys("Test")
    
  • xpath :

    driver.find_element_by_xpath("//input[@class='string required' and @id='name']").send_keys("Test")
    

更新

我已尝试寻找解决您遇到的两条错误消息的方法,以下是相关详细信息:

  • selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'. :根据讨论 possibly your application is based on Polymer and before you start looking out for any element soon after Page Load as a precautionary measure induce a waiter through WebDriverWait along with proper expected_conditions

  • Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'." :根据讨论 JS: Failed to execute 'getComputedStyle' on 'Window': parameter is not of type 'Element' and Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element' error 指出了类似的行为,即所需元素实际上并不存在于 DOM 而 JavaScriptAjax 调用 正在处理 adding/removing 个元素。