使用 Python 选择 iframe硒
所以,我对如何在 Selenium 中做到这一点感到非常困惑,并且在任何地方都找不到答案,所以我分享我的经验。
我试图选择一个 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 代码(使用 selenium 库)试图使用以下内容找到此 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 命令组合,重复失败。
偶尔的成功是不可复制的,所以也许这是某种竞争条件或其他什么?从来没有找到正确的方法将其放入硒中。
So, I was absolutely baffled as to how to do this in Selenium, and couldn't find the answer anywhere, so I'm sharing my experience.
I was trying to select an iframe and having no luck (or not repeatably anyway). The HTML is:
<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>
The Python code (using the selenium library) was trying to find this iframe using this:
@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")
Repeated fails with every combination of Selenium commands I could find.
The occasional success would not be reproducible, so perhaps it was some sort of race condition or something? Never did find the right way to get it in selenium proper.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这对我来说适用于 Python (v. 2.7)、webdriver 和 webdriver。 Selenium 在使用 iframe 进行测试并尝试在 iframe 中插入数据时:
This worked for me with Python (v. 2.7), webdriver & Selenium when testing with iframes and trying to insert data within an iframe:
如果
iframe
是动态节点,也可以显式等待iframe
出现,然后使用 < code>ExpectedConditions:如果
iframe
不存在具有@id
或@name
可以使用driver.find_element_by_xpath()
、driver.find_element_by_tag_name()< 作为常见 WebElement 找到它/code> 等:
从
iframe
切换回来:If
iframe
is dynamic node, it's also possible to wait foriframe
appearence explicitly and then switch to it usingExpectedConditions
:If
iframe
doesn't have@id
or@name
it can be found as common WebElement usingdriver.find_element_by_xpath()
,driver.find_element_by_tag_name()
, etc..:To switch back from
iframe
:最终对我有用的是:
基本上,不要使用 selenium 在 iframe 中查找链接并单击它;使用 jQuery。 Selenium 显然有能力运行任意一段 javascript(这是 python-selenium,我猜原来的 selenium 命令是 runScript 或其他东西),一旦我可以使用 jQuery,我就可以做这样的事情: 使用 jQuery 选择 iframe 中的表单
What finally worked for me was:
Basically, don't use selenium to find the link in the iframe and click on it; use jQuery. Selenium has the capability to run an arbitrary piece of javascript apparently (this is python-selenium, I am guessing the original selenium command is runScript or something), and once I can use jQuery I can do something like this: Selecting a form which is in an iframe using jQuery
您不需要使用JavascriptExecutor。您所需要做的就是切换到框架中,然后再切换回来,就像这样:
只要您小心谨慎,就永远不会有问题。我唯一一次总是使用 JavascriptExecutor 是为了获得窗口焦点,因为我认为在这种情况下使用 Javascript 更可靠。
You don't need to use JavascriptExecutor. All you needed to do was switch into the frame and then switch back out, like so:
As long as you are careful with this, you will never have a problem. The only time I always use a JavascriptExecutor is to get window focus since I think using Javascript is more reliable in that case.
在
中转移 Selenium 的焦点>
您可以使用以下任一定位器策略:使用
ID
:使用
CSS_SELECTOR
:使用
XPATH
:理想情况下,您必须诱导 WebDriver等待所需的框架可用并切换到它,您可以使用以下任一方法定位器策略:
使用
ID< /代码>:
使用
CSS_SELECTOR
:使用
XPATH
:注意:您必须添加以下导入:
参考
您可以在以下位置找到一些相关讨论:
To shift Selenium's focus within the
<iframe>
you can use either of the following Locator Strategies:Using
ID
:Using
CSS_SELECTOR
:Using
XPATH
:Ideally, you have to induce WebDriverWait for the desired frame to be available and switch to it and you can use either of the following Locator Strategies:
Using
ID
:Using
CSS_SELECTOR
:Using
XPATH
:Note : You have to add the following imports :
Reference
You can find a couple of relevant discussions in:
Selenium 的 selectFrame 命令接受所有标准定位器,如
css=
,但它还有一组额外的定位器,专门用于 FRAME 和 IFRAME 元素。正如 文档 所说:
一般来说,使用专用定位器,特别是如果您首先建立正确的上下文(例如、
select_frame("relative=top"); select_frame("id=upload_file_frame");
)。Selenium's selectFrame command accepts all the standard locators like
css=
, but it also has a an extra set of locators that work specifically with FRAME and IFRAME elements.As the doc says:
In general, you'll have better luck using the specialized locators, especially if you establish the right context first (e.g.,
select_frame("relative=top"); select_frame("id=upload_file_frame");
).来查找 iframe
您可以使用这个简单的代码来使用 xpath示例使用
set_iframe("/html/body/div[2]/table/")
you can use this simple code to look for the iframe using xpath
sample use
set_iframe("/html/body/div[2]/table/")