我如何使用Selenium和Python单击iframe中的按钮

问题描述 投票:0回答:4

我正在尝试在iframe中单击“管理”按钮,但出现此错误:

selenium.common.exceptions.TimeoutException: Message:

我正在使用的Python代码:

main = driver.find_element_by_xpath("//div[@class='main absolute']")
main.click()
driver.switch_to.frame("tab_Welcome")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wah-global-ask-banner-item div.wah-global-ask-banner-item-title.wah-global-ask-banner-item-title-paa")))
button.click()

HTML:

enter image description here

selenium xpath iframe css-selectors webdriverwait
4个回答
1
投票

得出WebDriverWaitframe_to_be_available_and_switch_to_it()产生WebDriverWaitelement_to_be_clickable()并遵循XPATH。

main = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='main absolute']")))
main.click()
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"frame_Welcome")))
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Administration']")))
button.click()

0
投票

似乎您正在使用其ID切换到iframe,但是您需要按名称切换到它。

所以代替了driver.switch_to.frame("tab_Welcome")

您应该尝试driver.switch_to.frame("frame_Welcome")

希望这会有所帮助。


0
投票

到文本为[[Administration的元素上的click(),因为所需元素在<iframe>中,因此您必须:

  • WebDriverWait设置为所需的[[框架可用并切换到它]]。WebDriverWait
  • 表示为所需的[[可点击的元素。
  • 您可以使用以下Locator Strategies中的任何一个:
  • CSS_SELECTOR

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe-content#tab_Welcome"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wah-global-ask-banner-item-title.wah-global-ask-banner-item-title-paa"))).click()
    • XPATH

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='iframe-content' and @id='tab_Welcome']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='wah-global-ask-banner-item-title wah-global-ask-banner-item-title-paa' and text()='Administration']"))).click()
    
  • :您必须添加以下导入:
  • from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • 在这里您可以找到有关Ways to deal with #document under iframe的讨论

    © www.soinside.com 2019 - 2024. All rights reserved.