为什么尝试使用 selenium 单击会出现“ElementNotInteractableException"? [英] Why does trying to click with selenium brings up "ElementNotInteractableException"?
问题描述
我正在尝试点击网页
因此链接位于 div 内,当前不可见(实际上整个子部分从 div
开始,带有 id="hqList"
是隐藏的).Selenium 不允许您单击不可见的元素,尽管它允许您检查它们.因此,获取元素有效,点击它 - 无效.
你用它做什么取决于你的期望是什么.在这种特殊情况下,您似乎需要单击 <label class="branches-map__toggle-label" for="branchesToggle">Список</label>
才能显示该链接.所以添加这个:
browser.find_element_by_link_text("Список").click();
之后,您可以点击列表中的任何链接.
I'm trying to click on the webpage "https://2018.navalny.com/hq/arkhangelsk/" from the website's main page. However, I get this error
selenium.common.exceptions.ElementNotInteractableException: Message:
There's nothing after "Message:"
My code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get('https://2018.navalny.com/')
time.sleep(5)
linkElem = browser.find_element_by_xpath("//a[contains(@href,'arkhangelsk')]")
type(linkElem)
linkElem.click()
I think xpath is necessary for me because, ultimately, my goal is to click not on a single link but on 80 links on this webpage. I've already managed to print all the relevant links using this :
driver.find_elements_by_xpath("//a[contains(@href,'hq')]")
However, for starters, I'm trying to make it click at least a single link.
Thanks for your help,
The best way to figure out issues like this, is to look at the page source using developer tools of your preferred browser. For instance, when I go to this page and look at HTML tab of the Firebug, and look for //a[contains(@href,'arkhangelsk')]
I see this:
So the link is located within div, which is currently not visible (in fact entire sub-section starting from div
with id="hqList"
is hidden). Selenium will not allow you to click on invisible elements, although it will allow you to inspect them. Hence getting element works, clicking on it - does not.
What you do with it depends on what your expectations are. In this particular case it looks like you need to click on <label class="branches-map__toggle-label" for="branchesToggle">Список</label>
to get that link visible. So add this:
browser.find_element_by_link_text("Список").click();
after that you can click on any links in the list.
这篇关于为什么尝试使用 selenium 单击会出现“ElementNotInteractableException"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!