find_element_by_class_name不适用于python

时间:2018-04-03 10:03:34

标签: python selenium

我正在尝试使用selenium和python填写表单。

我在以下代码片段中使用了find_element_by_class_name和find_element_by_css_selector,但它无效。我不知道为什么和java一样有效......

代码:

url='https://service.mail.com/registration.html'
driver = webdriver.Chrome('/Users/xxxxx/code/chromedriver')
driver.get(url)
driver.find_element_by_css_selector('.Required.userdata-firstname').send_keys("james")

结果:

Traceback (most recent call last):
  File "captcha_createMailBoxes.py", line 51, in <module>
    driver.find_element_by_css_selector('.Required.userdata-firstname').send_keys("james")
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <li class="Required userdata-firstname"> is not reachable by keyboard

代码:

url='https://service.mail.com/registration.html'
driver = webdriver.Chrome('/Users/xxxxx/code/chromedriver')
driver.get(url)
driver.find_element_by_class_name("userdata-firstname").send_keys("james")

结果:

Traceback (most recent call last):
  File "captcha_createMailBoxes.py", line 54, in <module>
    driver.find_element_by_class_name("userdata-firstname").send_keys("james")
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <li class="Required userdata-firstname"> is not reachable by keyboard

有人可以帮忙吗?我真的不想和java一起工作.....

2 个答案:

答案 0 :(得分:0)

find_element_by_class_name接收一个类作为参数。 "Required userdata-firstname"是两个班级。要么使用其中一个

driver.find_element_by_class_name('Required')
# or
driver.find_element_by_class_name('userdata-firstname')

如果您想要两个

,请使用css_selector
driver.find_element_by_css_selector('.Required.userdata-firstname')

答案 1 :(得分:0)

您在示例中引用的CSS元素是li元素。你不能用文本填充li元素。您需要填写input字段。这些字段随机设置value_name个属性,以防止机器人使用它们。您可以尝试使用id属性。

driver.find_element_by_id('idf').send_keys('james')

编辑:如果id改变,你也可以试试xpath。你可以这样做:

driver.find_element_by_xpath('//li[@class="Required userdata-firstname"]/div/span/input').send_keys('james')