Python Selenium:Send_keys无法执行

时间:2018-04-26 12:53:32

标签: python-3.x selenium selenium-chromedriver sendkeys traceback

我想用

填写表格
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)

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

注意:我在搜索名称时也会收到错误消息。如果我搜索Xpath,它会非常慢地填写表单,我需要它更快。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用定位器策略中的任何一个:

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

更新

我试图寻找您遇到的两个错误消息的解决方案,以下是相关详细信息: