我知道这个问题已经被问过很多次了,但是您如何解决“元素难以处理”的异常呢?
我对Selenium还是很陌生,所以如果我发现错误或误解,请原谅。
我尝试在代码的各个部分中添加time.sleep(20),以查看这是否允许元素加载,但目前还没有成功。
我在这里想念东西吗?
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
#Login Credentials
email = 'anexample@fakeemail.com'
password = 'Password123'
#Login to Money Dashboard
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get("https://my.moneydashboard.com/")
loginPageEmail = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="input_0"]')))
loginPageEmail.send_keys(email)
我总是会遇到以下错误:
Traceback (most recent call last):
File "mdash.py", line 26, in <module>
loginPageEmail.send_keys(email)
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 633, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 321, 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 not interactable
(Session info: chrome=75.0.3770.100)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac
OS X 10.13.6 x86_64)
答案 0 :(得分:1)
如果您每次运行代码时都检查元素ID attribute
是否是动态的,那么这里的名称attribute
应该是unique attribute
才能访问input
元素。表单元素不能识别input
元素,因此我将表单元素与输入元素和输入元素的唯一属性一起使用。
使用WebdriverWait
和elementtobeclickable
,然后跟随xpath
。
email = 'anexample@fakeemail.com'
password = 'Password123'
#Login to Money Dashboard
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get("https://my.moneydashboard.com/")
loginPageEmail = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//form[@name="vm.registerForm"]//div[@class="inputs"]//input[@name="email"]')))
loginPageEmail.send_keys(email)
loginPagepassword = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//form[@name="vm.registerForm"]//div[@class="inputs"]//input[@name="password"]')))
loginPagepassword.send_keys(password)