Selenium Python Chrome – “元素不可交互”

huangapple go评论167阅读模式
英文:

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个结果。第一个不会突出显示所需的文本框,但第二个会。试试看。

Selenium Python Chrome – “元素不可交互”

英文:

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=&#39;signInFormUsername&#39;])[2]

Code:

login_form = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, &quot;(//input[@id=&#39;signInFormUsername&#39;])[2]&quot;)))
login_form.send_keys(&#39;User&#39;)

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=&#39;signInFormUsername&#39;]. 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.

Selenium Python Chrome – “元素不可交互”

答案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')

英文:

Selenium Python Chrome – “元素不可交互”
There are two elements that the website switches between in small or large size,
so if large try
driver.find_elements(By.CSS_SELECTOR, &quot;form input[id=&#39;signInFormUsername&#39;]&quot;)[0].send_keys(&#39;User&#39;)

or

driver.find_elements(By.CSS_SELECTOR, &quot;form input[id=&#39;signInFormUsername&#39;]&quot;)[1].send_keys(&#39;User&#39;)

huangapple
  • 本文由 发表于 2023年8月10日 14:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定