16
因此,我对Selenium如何做到这一点感到十分困惑,并且无法在任何地方找到答案,所以我分享了我的经验。使用python selenium选择iframe
我试图选择一个iframe,并没有运气(或无法重复)。该HTML是这样的:
<iframe id="upload_file_frame" width="100%" height="465px" frameborder="0" framemargin="0" name="upload_file_frame" src="/blah/import/">
<html>
<body>
<div class="import_devices">
<div class="import_type">
<a class="secondary_button" href="/blah/blah/?source=blah">
<div class="import_choice_image">
<img alt="blah" src="/public/images/blah/import/blah.png">
</div>
<div class="import_choice_text">Blah Blah</div>
</a>
</div>
</div>
</body>
</html>
的Python代码(使用硒库)试图以此来找到这个iframe中:
@timed(650)
def test_pedometer(self):
sel = self.selenium
...
time.sleep(10)
for i in range(5):
try:
if sel.select_frame("css=#upload_file_frame"): break
except: pass
time.sleep(10)
else: self.fail("Cannot find upload_file_frame, the iframe for the device upload image buttons")
反复失败,硒命令的每个组合我能找到。偶尔的成功不会是可重复的,也许这是某种竞争条件或某种事情?从来没有找到正确的方法让它适应硒。
你最好使用Selenium的getEval命令(可能是Python绑定中的“get_eval”)而不是其runScript命令。 getEval在JavaScript代码完成之前不会返回,而runScript简单插入
Selenium的selectFrame命令接受所有标准定位器,如
css=
,但它也有一组额外的定位器,专门用于FRAME和IFRAME元素。作为the doc说:
一般来说,你使用专业定位器会有更好的运气,尤其是如果您先建立正确的环境(,例如,
select_frame("relative=top"); select_frame("id=upload_file_frame");
)。来源
2011-09-24 14:31:05
是的,selectFrame没有为我工作。这并不意味着它不适用于任何人,但我发现它实际上是在5中选择了一次。 – rossdavidh
你不需要使用JavascriptExecutor。你需要做的全部是开关到框架,然后再切换回来了,就像这样:
只要你小心一点,你将永远不会有问题。我总是使用JavaScriptExecutor的唯一时间是获得窗口焦点,因为我认为在这种情况下使用Javascript更可靠。
来源
2013-05-09 23:42:06 djangofan
这对Python有用(v。2.7),借助iframe测试,并试图在iframe中插入数据时的webdriver &硒:
来源
2014-06-18 13:05:38
不知何故,当用xpath搜索时,WebDriver会将元素放入iframe中。我试过通过CSS,ID找到元素,但它总是给我一个不存在元素的错误(实际上因为我使用了waitForElement命令而导致超时失败)。我没有想到用xpath搜索元素会有所作为,但幸运的是,当我用完了想法之后,尝试了一下。非常感谢! – GOstrowsky
'driver.switch_to_frame()'现在不推荐使用,请使用'driver.switch_to.frame()' –
这里是一个工作的教程,帮助您了解如何处理使用硒的webdriver与Python网页上的I帧: https://www.youtube.com/watch?v=sfdWF4vBZ2o
这个命令是用来处理I帧: driver.switch_to.frame(frame_id)
来源
2016-12-10 19:42:17
如果
iframe
是动态的节点,它也可以等待iframe
appeare NCE明确,然后切换它使用ExpectedConditions
到:如果
iframe
没有@id
或@name
可以发现如使用driver.find_element_by_xpath()
,driver.find_element_by_tag_name()
等共同WebElement ..:要从
iframe
转回:来源
2018-02-11 08:19:11 Andersson