我必须找到iframe,我的html源代码看起来像
<iframe border="0" scrolling="yes" src="problem_list.do?sysparm_query=u_service_ticket_triage_group.name!%3Dxbt%20tech%20support%5Eu_program.name%3DTX_STAAR%5Eu_reject_ticket!%3Dtrue" name="TribName" width="100%" allowtransparency="true" style="width: 100% !important; height: 800px !important;" frameborder="0" id="TribID"></iframe>
我尝试使用id和name来找到这个iframe,但我收到了错误
&#34;没有这样的元素:无法找到元素:&#34;
以下是我试过的一次
iframe = driver.find_element_by_name("TribName")
driver.switch_to.frame(iframe)
iframe = driver.find_element_by_id("TribID")
driver.switch_to.frame(iframe)
你能否就我如何选择这个iframe提出建议。
答案 0 :(得分:1)
iframe可能会动态生成,因此您可能需要明确等待其外观:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("TribID"))
如果是嵌套框架,例如
<iframe id="OuterFrame">
<iframe id="TribID"></iframe>
</iframe>
你应该从高级祖先开始按顺序切换到每一帧:
driver.switch_to.frame("OuterFrame")
driver.switch_to.frame("TribID")
答案 1 :(得分:0)
根据您提供的用于切换到所需框架的HTML,您需要使用 WebDriverWait 使框架可用,然后使用以下任一方法切换:
通过 Frame ID :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"TribID")))
通过框架名称:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"TribName")))