【问题标题】:Selenium WebDriver -- Can't get into an iframe with dynamic IDSelenium WebDriver - 无法进入具有动态 ID 的 iframe
【发布时间】:2018-06-11 14:13:05
【问题描述】:

使用 Selenium,我尝试访问 iframe 内的元素。

这是 HTML 代码:

<iframe id="DWT54_content_ifr" src='javascript:""' frameborder="0" 
allowtransparency="true" title="Texte richeFaites ALT-F10 pour accéder à la 
barre d'outils. Faites ALT-0 pour accéder à l'aide" style="width: 100%; 
height: 0px; display: block;">

<body id="tinymce" class="mceContentBody " 
onload="window.parent.tinyMCE.get('DWT54_content').onLoad.dispatch();" 
contenteditable="true" dir="LTR" style="font-family: &quot;times new 
roman&quot;, &quot;new york&quot;, times, serif; font-size: 14pt; color: 
rgb(0, 0, 0);"><div><br></div></body></iframe>

这是我进入 iframe 的 Python 代码:

time.sleep(3)
driver.switch_to_frame(driver.find_element_by_id("DWT54_content_ifr"))

I also tried these codes:
driver.switch_to_frame("DWT54_content_ifr")

and

driver.switch_to_frame(driver.find_element_by_xpath("//iframe[@id='DWT54_content_ifr']"))

但每次我得到同样的错误:“无法定位元素”。

更新:

我设法使用这行代码进入 iframe:

driver.switch_to_frame(3)

但这不是很方便,因为我必须手动计算页面中 iframe 的数量...

有什么帮助吗?

谢谢。

【问题讨论】:

  • 试试这个选择器iframe[id$='_content_ifr'],以防每次随机生成DWT54。也等待元素可见性。
  • 能否分享完整的html代码?可能是在某个框架内
  • 小更新:感谢“driver.switch_to_frame(3)”,我成功地进入了我的 iframe。但是我不得不手动计算 iframe 的数量......这不是很方便。图尔西亚,我用了你的方法,结果还是一样。
  • @AnkurSingh:我检查过了,它不在其他框架内

标签: python html selenium iframe selenium-webdriver


【解决方案1】:

根据HTML,您共享了@98​​7654322@ 属性的值,即DWT54 在我看来是动态的。所以我们必须构造一个动态的cssxpath来识别frame,而不是driver.switch_to_frame(driver.find_element_by_id(frame_reference)),我们必须使用driver.switch_to.frame(frame_reference),如下所示:

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@id,'_content_ifr') and contains(@title,'Texte richeFaites')]"))

【讨论】:

    猜你喜欢
    • 2017-05-30
    • 2014-06-09
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    相关资源
    最近更新 更多