使用python 3.7.4,最新的selenium和geckodriver和Firefox 69.0.1版。
im试图简单地在Google主页上的“即时感觉很幸运”按钮上使用click(),但出现错误selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
。
我尝试使用msg_box.location_once_scrolled_into_view
和get_element_by_class / id / name无效。这是代码:
from selenium import webdriver
import time
url = 'https://www.google.com'
driver = webdriver.Firefox()
driver.get(url)
time.sleep(4)
msg_box = driver.find_element_by_class_name('RNmpXc')
msg_box.location_once_scrolled_into_view
time.sleep(1)
msg_box.click()
什么可能导致错误?
消息
selenium.common.exceptions.ElementNotInteractableException:消息:无法将元素滚动到视图中。
表示当前无法选择。您的程序正在尝试与之交互,但是无法滚动到视图中。可能是程序正在尝试查找该元素,但是随后它(例如)“消失”(例如弹出或出现一个新按钮),因此无法滚动到视图中,因此无法单击。
您可以添加等待条件(当出现弹出窗口时),它会在弹出窗口消失后等待几秒钟,即单击该元素。
mySelectElement = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,“ EXAMPLE”))))mySelectElement.click()
注意:我添加了示例。