【问题标题】:clicking on button in iframe with python用python点击iframe中的按钮
【发布时间】:2020-12-11 20:22:59
【问题描述】:

我正在尝试使用 python selenium 访问网站。我要放置我无法点击按钮的地方。请问有什么帮助吗? 请在下面找到我的代码:

from selenium import webdriver
from time import sleep

path_to_chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(path_to_chromedriver)

url = 'https://www.mail.com/'
driver.get(url)

如何使用css selector点击按钮"Agree and Continue"

【问题讨论】:

    标签: python selenium selenium-webdriver css-selectors webdriverwait


    【解决方案1】:

    元素 Agree and Continue 存在于 nested iframe 中,您需要先切换两个 iframe,然后单击该元素。

    诱导WebDriverWait()并等待frame_to_be_available_and_switch_to_it() 诱导WebDriverWait(),等待element_to_be_clickable()

    driver.get("https://www.mail.com/")
    WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
    WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.mail']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']"))).click()
    

    您需要导入以下库。

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

    跳出你需要使用的iframe。

    driver.switch_to.default_content()
    

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 2023-04-09
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2016-05-08
      • 2021-09-14
      相关资源
      最近更新 更多