无法正确执行 click()

can not execute click() properly

我正在使用 python 3.7.4、最新的 selenium 和 geckodriver,以及 Firefox 版本 69.0.1。

我试图简单地在 google 主页的 'im feeling lucky' 按钮上使用 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: Message: Element could not be scrolled into view.

表示目前无法选择。您的程序正在尝试与其交互,但它无法滚动到视图中。可能是例如该程序正在尝试查找该元素,但随后它(示例)"disappears"(例如弹出或出现新按钮)因此无法滚动到视图中,因此无法单击。

您可以添加等待条件(当弹出窗口出现时)它在弹出窗口消失后等待几秒钟,它正在单击元素。

msgBox02 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "EXAMPLE"))) msgBox02.click()

注意:我添加了与 RNmpXc 相同的示例

诱导 WebDriverWaitpresence_of_element_located()

诱导 javascript executor。但是网络驱动程序点击按钮不起作用。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

url = 'https://www.google.com'
driver = webdriver.Firefox()
driver.get(url)
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,'RNmpXc')))
driver.execute_script("arguments[0].click();", element)