【发布时间】:2018-07-04 13:51:02
【问题描述】:
我有以下页面
<body>
<iframe id="outer_frame">
<iframe id="search_button_frame">
<button id="search_button"></button>
</iframe>
</iframe>
</body>
现在我点击search_button 后,两个frames outer_frame 和search_button_frame 不再在DOM 中,而是页面变成了这样
<body>
<iframe id="form_frame">
...
</iframe>
</body>
我正在尝试转到search_button,然后跳出框架进入主页,然后使用以下命令切换到form_frame:
outer_frame = browser.find_element_by_xpath('//*[@id="outer_frame"]')
browser.switch_to.frame(outer_frame)
search_button_frame = browser.find_element_by_xpath('//*[@id="search_button_frame"]')
browser.switch_to.frame(search_button_frame)
search_button = browser.find_element_by_xpath('//*[@id="search_button"]')
search_button.click()
browser.switch_to_default_content()
form_frame = browser.find_element_by_xpath('//*[@id="form_frame"]')
browser.switch_to.frame(form_frame)
但我不断收到以下错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Una
ble to locate element: {"method":"xpath","selector":"//*[@id="form_frame"]"}
这是否意味着我没有定位在正确的框架中?
【问题讨论】:
-
IMO,你做得很好。在切换到 form_frame 之前添加一些等待,并将所有 xpath 替换为 id。
标签: python selenium selenium-webdriver iframe webdriver