Selenium -无法单击此网站弹出按钮上的()

o4hqfura  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(137)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

browser = webdriver.Chrome()
browser.get('https://www.chefkoch.de/')
WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(text(),'Akzeptieren und weiter')]"))).click()

presence_of_element_located而不是clickable,通过CSS_SELECTOR,实际的XPATH或实际的XPATH在它的全长,没有一个工作。
我只想单击第一个弹出窗口的左侧按钮()。

编辑

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

browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.chefkoch.de/')

WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='sp_message_iframe_847995']")))
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"(//button[text()='Zustimmen'])[1]"))).click()
browser.switch_to.default_content()

WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="w-header"]/div[1]/div/div[2]'))).click()

到目前为止一切顺利。。下一个叠加图来了这一个似乎不是一个iframe,而且无论我尝试什么,都不能与这个容器上/中的任何东西交互
我对输入字段很感兴趣

smdnsysy

smdnsysy1#

假设这是您要单击的按钮:

在按钮上执行click()之前,您需要先切换到IFRAME。参考以下工作代码:

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

browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.chefkoch.de/')

# Switch into the IFRAME
WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='sp_message_iframe_847995']")))

# Click on Zustimmen button
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"(//button[text()='Zustimmen'])[1]"))).click()

# Come out of IFRAME
browser.switch_to.default_content()

相关问题