在 selenium 中更改为 iframe

Change to iframe in selenium

https://www.gmx.net

我想点击Zustimmen und weiter

driver.get('https://www.gmx.net')
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '/html/body/iframe')))
driver.find_element_by_xpath('//*[@id="save-all-conditionally"]').click()

上面的代码对我不起作用。

您尝试访问的元素位于嵌套的 iframe 中,一个 iframe 插入另一个 iframe。
您应该切换到第一个 iframe,然后切换到内部 iframe,然后单击“获取 cookie”按钮。
这应该有效:

wait = WebDriverWait(driver, 20)
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[@class="permission-core-iframe"]')))
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src,'plus')]")))
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="save-all-conditionally"]'))).click()

当您完成在该 iframe 中的工作后,您必须使用

切换到默认上下文
driver.switch_to.default_content()