在Selenium中如何点击iframe下的元素。层级关系:iframe>#document>HTML。已添加截图
我切换到了那个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