我正在try 点击BTN"SELECT ALL COOKIES"on Cookies Terms弹出窗口. 我试过了:

div=driver.find_element(By.XPATH,'//*[@id="js_cookie-settings-modal"]/div[2]/div/div[3]/div')
driver.implicitly_wait(4)
button=div.find_element(By.XPATH,'//*[@id="selectAll"]')
driver.implicitly_wait(4)
button.click()

错误:

  raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=111.0.5563.64)

选项2:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="selectAll"]'))).click()
driver.implicitly_wait(50)

错误:

   raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

网址:https://www.eversports.de/

推荐答案

如果使用下面的xpath //*[@id="selectAll"]',它将标识两个元素,其中第一个不是interactable.

使用低于xpathuniquely identify的元素. 你不需要使用parent元素,你可以用这xpath元素来识别并点击按钮.

button=driver.find_element(By.XPATH,'//button[@id="selectAll" and text()="Accept all"]')
button.click()

理想情况下,您应该使用WebDriverWait()来处理动态页面元素.

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="selectAll" and text()="Accept all"]'))).click()

你需要跟随进口货.

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

Update:

driver.get("https://www.eversports.de/")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="selectAll" and text()="Accept all"]'))).click()

有可能第一次出现cookie个选项,一旦接受,它就永远不会出现.使用Try..Expect块来处理此问题.

driver.get("https://www.eversports.de/")
try:
     WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="selectAll" and text()="Accept all"]'))).click()
except:
     print("No popup found!")
     pass

Python相关问答推荐

如何根据日期和时间将状态更新为已过期或活动?

根据条件将新值添加到下面的行或下面新创建的行中

如何将ctyles.POINTER(ctyles.c_float)转换为int?

'discord.ext. commanders.cog没有属性监听器'

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

如何记录脚本输出

Python中绕y轴曲线的旋转

将tdqm与cx.Oracle查询集成

将9个3x3矩阵按特定顺序排列成9x9矩阵

实现自定义QWidgets作为QTimeEdit的弹出窗口

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

为什么\b在这个正则表达式中不解释为反斜杠

如何在TensorFlow中分类多个类

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

pandas:对多级列框架的列进行排序/重新排序

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

如何设置nan值为numpy数组多条件

递归链表反转与打印语句挂起

如何使用count()获取特定日期之间的项目