英文:
Selenium Python Chrome - "element not interactable"
问题
在这个页面,尝试并失败地向用户名和密码文本字段中输入文本:
我能够选择元素,但它会显示"不可交互"的消息。
我已经尝试过:
#1
login_form = driver.find_element(By.CSS_SELECTOR, "input[id='signInFormUsername']")
login_form.send_keys('User')
#2
login_form = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='signInFormUsername']")))
login_form.send_keys('User')
是否有其他方法可以将文本发送到这个元素?
英文:
Trying and failing to input text into the username and password text fields on this page:
I'm able to select the element but it throws the not interactable message.
I've tried:
#1
login_form = driver.find_element(By.CSS_SELECTOR, "input[id='signInFormUsername']")
login_form.send_keys('User')
#2
login_form = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='signInFormUsername']")))
login_form.send_keys('User')
Any other means of sending text to this element?
答案1
得分: 1
你有2个登录元素,而实际上返回find_element
的第一个元素是不可见的。
所以,为了解决你的问题,你应该按可见元素进行筛选,并从数组中获取第一个可见元素。
login_elements = driver.find_elements(By.CSS_SELECTOR, '[name=username]')
login_input = [element for element in login_elements if element.is_displayed()][0]
login_input.send_keys('User')
英文:
You have 2 login elements and first element that actually returns find_element
is invisible.
So, to solve your issue, you should filter by visible elements and get first visible element from array.
login_elements = driver.find_elements(By.CSS_SELECTOR, '[name=username]')
login_input = [element for element in login_elements if element.is_displayed()][0]
login_input.send_keys('User')
答案2
得分: 0
根本原因: 有2个具有@id=signInFormUsername
的元素,您需要识别并定位第二个元素。您的代码尝试定位第一个元素,而第一个元素在浏览器上不可见,因此您会收到Element not interactable
异常。
解决方案: 使用XPath定位策略,使用以下XPath表达式来定位DOM中的第二个元素:
(//input[@id='signInFormUsername'])[2]
代码:
login_form = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "(//input[@id='signInFormUsername'])[2]")))
login_form.send_keys('User')
更新: 如何确定XPath表达式是否定位到一个或多个元素:
按键<kbd>F12</kbd> --> 在控制台上按<kbd>Ctrl + F</kbd>,然后粘贴XPath表达式 //input[@id='signInFormUsername']
。您会注意到如下屏幕截图所示的2个结果。第一个不会突出显示所需的文本框,但第二个会。试试看。
英文:
Root cause: There are 2 elements with @id=signInFormUsername
, you need to identify and locate the second element. Your code is trying to locate the first element, and the first element is not visible on browser, hence you are getting Element not interactable
exception.
Solution: Using XPath locator strategy, use the below XPath expression to locate second element in the DOM:
(//input[@id='signInFormUsername'])[2]
Code:
login_form = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "(//input[@id='signInFormUsername'])[2]")))
login_form.send_keys('User')
UPDATE: How to find if XPath expression locates one or more elements:
Press key <kbd>F12</kbd> --> Press <kbd>Ctrl + F</kbd> on console and paste the XPath expression //input[@id='signInFormUsername']
. You will notice 2 results as shown in below screenshot. First one won't highlight on the desired text box, but second one will. Try it.
答案3
得分: -1
driver.find_elements(By.CSS_SELECTOR, "form input[id='signInFormUsername']")[0].send_keys('User')
或
driver.find_elements(By.CSS_SELECTOR, "form input[id='signInFormUsername']")[1].send_keys('User')
英文:
There are two elements that the website switches between in small or large size,
so if large try
driver.find_elements(By.CSS_SELECTOR, "form input[id='signInFormUsername']")[0].send_keys('User')
or
driver.find_elements(By.CSS_SELECTOR, "form input[id='signInFormUsername']")[1].send_keys('User')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论