Python selenium 无法在 iframe 中定位元素

Python selenium unable to locate element in iframe

我很困惑为什么我不能 select 这个 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> == [=11=]

这是我的 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"}

请有人帮助我。 非常感谢。

不能直接切换到子框架。 切换到第二帧,如下代码:

driver.switch_to.frame("qqAuthFrame_8639242")
driver.switch_to.frame("ptlogin_iframe")

或者尝试使用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")))

# This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds.