在Selenium中如何点击iframe下的元素。层级关系:iframe>#document>HTML。已添加截图

0 投票
1 回答
20 浏览
提问于 2025-04-13 13:08

在这里输入图片描述

我切换到了那个iframe,但还是无法访问HTML下面的元素。请注意,#document没有CSS选择器或XPath,我觉得它不是一个元素。谢谢你们的提前帮助。

1 个回答

1

你可以使用 By.ID 这个定位策略,方法如下:

# To switch into the desired IFRAME
iframe = driver.find_element(By.ID, "remote_iframe_0")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(iframe))

# perform desired actions here within the IFRAME

# To come out of iframe
driver.switch_to.default_content()

需要导入的内容:

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

撰写回答