我对为什么不能在此iframe下选择任何元素感到非常困惑。
HTML是这样的:html code
我在这里看到两个iframe,一个在另一个。我需要转到内部iframe下的“ switcher_plogin”。
相关的html代码是:
<a class="link" hidefocus="true" id="switcher_plogin" href="javascript:void(0);" tabindex="8">text here</a> == $0
这是我的python代码:
driver.switch_to.frame(1) # to switch to the second iframe
driver.find_element_by_id('switcher_plogin').click()
但是错误说:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".bottom hide"}
[请有人帮我。非常感谢。
尝试使用WebDriverWait等待帧可用于切换。
并像下面的代码一样切换到第二帧:
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "qqAuthFrame_8639242")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "ptlogin_iframe")))
此操作最多等待10秒,然后抛出TimeoutException,除非它发现要在10秒内返回的元素。