小能豆

我怎样才能选择一个 html 元素,无论它位于 selenium 中的哪个框架中?

javascript

我正在尝试选择位于 iframe 内且可能位于其他 iframe 中的元素。

是否有可能在 (python)selenium 中不先选择 iframe 而选择某个 (子)iframe 中的元素?有没有办法以某种方式“循环”遍历每个 iframe 并检查在哪里可以找到我的元素……?

那么,在元素、 html 内容和 iframe 即将被加载的情况下,该怎么做呢……?


阅读 61

收藏
2024-07-15

共1个答案

小能豆

循序渐进的方法:

  1. 切换到每个 iframe 并检查元素。
  2. 通过递归切换和检查来处理嵌套的 iframe。
  3. 使用 WebDriverWait 处理元素的动态加载。

示例代码

下面是一个示例函数,演示了如何实现这一点:

from selenium import webdriver
from selenium.webdriver.common.by import By

f
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def find_element_in_iframes(driver, locator):
    def recursive_search_in_iframes(driver):
        iframes = driver.find_elements(By.TAG_NAME, 
        iframes = driver.find_elements(By.TAG_NA
"iframe")
        for i, iframe in enumerate(iframes):
            driver.switch_to.frame(iframe)

            driver.switch_to.frame(ifr
try:
                element = WebDriverWait(driver, 
                elemen
2).until(
                    EC.presence_of_element_located(locator)
                )

                    EC.presence_of_element_located(locator)


                    EC.presence_of_eleme


return element
            except:
                pass


# Recursively search in nested iframes
            element = recursive_search_in_iframes(driver)

            element = recursive_search_in_iframes(driver)


            element = recursive_search_in_iframes(dri

            element = recursive_searc

            elemen
if element:

                ret


return element
            driver.switch_to.parent_frame()

            driver.switch_to.parent_frame()
        retur

            driver.switch_to.parent_frame()


            driver.switch_to.parent_frame()


            driver.switch_to.parent_fram

            driver.switch_to.par

            driver.switc

            driv


return None

    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(original_window)


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(original_window)
    retur


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(original_window)
    r


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(original_window)



    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(original_window)


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(original_w


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.window(orig


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to.windo


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.switch_to


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driver.swi


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)
    driv


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver)



    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(driver


    original_window = driver.current_window_handle
    element = recursive_search_in_iframes(


    original_window = driver.current_window_handle
    element = recursive_search_in_if


    original_window = driver.current_window_handle
    element = recursive_search


    original_window = driver.current_window_handle
    element = recursive_


    original_window = driver.current_window_handle
    element = recu


    original_window = driver.current_window_handle
    element =


    original_window = driver.current_window_handle
    elem


    original_window = driver.current_window_handle



    original_window = driver.current_window_handl


    original_window = driver.current_window_


    original_window = driver.current_wi


    original_window = driver.curr


    original_window = drive


    original_window =


    original_wi


    origi



return element

# Example usage
driver = webdriver.Chrome()
driver.get(
driver = webdriver.Chrome()
driver

driver = webdriver.Chrome()
dr

driver = webdriver.Chrome(

driver = webdriver.Chr

driver = webdriver

driver = webdr

driver = w

driver

d
"your_webpage_url")

# Define the locator for the element you are looking for
locator = (By.CSS_SELECTOR, "your_css_selector")

# Find the element within iframes
element = find_element_in_iframes(driver, locator)
if element:
    print("Element found:", element)
else:
    print("Element not found")

driver.quit()

解释

  1. 初始化 WebDriver:启动 WebDriver 并导航到所需的 URL。

  2. 定义find_element_in_iframes函数

:该函数:

  • 定义一个recursive_search_in_iframes辅助函数来处理嵌套的 iframe。
  • 在当前页面上搜索 iframe,切换到每个 iframe,并尝试找到所需的元素。
  • 如果未找到该元素,它会在嵌套的 iframe 内递归搜索。
  • 如果找到则返回该元素,None如果未找到则返回该元素。

  • 用法:定义要查找的元素的定位器,并find_element_in_iframes使用驱动程序和定位器调用该函数。

处理动态加载

为了处理动态加载,该函数使用WebDriverWaitwith presence_of_element_located。您可以根据具体要求调整等待时间和条件。

笔记

确保locator要查找的元素的 定义正确。此方法可帮助您在多层 iframe 中搜索元素,而无需手动切换到每一层。

2024-07-15