Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium WebDriver—Can';无法进入具有动态ID的iframe_Python_Html_Selenium_Iframe_Selenium Webdriver - Fatal编程技术网

Python Selenium WebDriver—Can';无法进入具有动态ID的iframe

Python Selenium WebDriver—Can';无法进入具有动态ID的iframe,python,html,selenium,iframe,selenium-webdriver,Python,Html,Selenium,Iframe,Selenium Webdriver,使用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

使用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:

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

需要帮忙吗


谢谢。

根据
HTML
您已经共享了
id
属性的值,即
DWT54
在我看来是动态的。因此,我们必须构造一个动态的
css
xpath
来识别
frame
而不是
驱动程序。切换到\u frame(driver.find\u element\u by_id(frame\u reference))
我们必须使用
驱动程序。切换到.frame(frame\u reference)
如下:

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

如果DWT54每次都是随机生成的,请尝试此选择器
iframe[id$=''u content\u ifr']
。放置并等待元素可见性。可能存在重复的。请共享完整的html代码好吗?可能是在某个框架内的小更新:由于“driver.switch_to_frame(3)”,我成功地进入了我的iframe。但我必须手动计算iframe的数量。。。这不是很方便。图尔西亚,我应用了你的方法,但结果是一样的。@AnkurSingh:我已经检查过了,它不在其他框架内
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@id,'_content_ifr') and contains(@title,'Texte richeFaites')]"))