python - 使用 Selenium webdriver 在 Python 中定位 iframe

标签 python selenium iframe

我必须找到 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 和名称来定位此 iframe,但出现错误

"no such element: Unable to locate element:"

以下是我试过的一次

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。

最佳答案

根据您提供的要切换到所需框架的 HTML,您需要使用 WebDriverWait 使框架可用,然后使用以下任一方法切换:

  • 通过帧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")))
    

关于python - 使用 Selenium webdriver 在 Python 中定位 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49778017/

相关文章:

javascript - 将标题从主页设置为 iframe

iframe - 如何获取 chrome 扩展以加载包含本地页面的 iframe

数据文件(非模块)的 python 搜索路径 - 文本文件 (*txt)

python - 使用 Pandas 如何计算数据组?

python - 名称错误 : name 'BillPayer' is not defined

python - 是否有可能 "transfer"selenium.webdriver 和 requests.session 之间的 session

angularjs - Selenium Webdriver和angular e2e有什么区别?何时应分别使用?

java - 在 jenkins 中使用 Docker 从站时,Selenium 驱动程序崩溃并给出 NoSuchSessionException

javascript - 如何重新加载 iframe 的父级而不重新加载它

python - pyicu 无法在 Mountain Lion 上链接 icu4c