iframe下#document的处理方式 [英] Ways to deal with #document under iframe

查看:62
本文介绍了iframe下#document的处理方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我现在正在测试的门户,我遇到了无法创建任何 xpath 定位器的问题,一段时间后我发现这是因为#document",这会切断路径并简化复制 xpath"将路径指向一个完全不同的元素.

For the portal I am testing now, I came with the problem that I could not create any xpath locators, after some time I figured out that it was because of an '#document', this cuts the path and makes the simple "copy xpath" to direct the path to a completely different element.

<iframe id="FRAMENAME" src="/webclient/workspace/launch-task/REMbl?ds=BP" width="100%" height="100%" frameborder="0" data-navitemname="navitemname" style="" xpath="1">
#document
    <html>
        CODE....
    </html>

我找到了解决方案,只需像这样添加一个 switchTo:

I found the solution for this is it is simply add a switchTo like this:

driver.switchTo().frame("FRAMENAME");

这有效并使其余代码正常工作,但需要一些额外的时间来处理此命令,直到代码移动到下一行.

This works and makes the rest of the code to work properly but, takes some extra time processing this command till the code moves to the next line.

所以我想问一下,有没有更好的解决方案?更智能/更快的东西?

So I would like to ask, is there is a better solution for this? something smarter/faster?

我担心当我有很多脚本的时候,执行时间会太长.

I am concerned that when the point where I have lots of scripts comes, the execution time will take too long.

例如,我不使用 id 定位器,因为它们都是动态的,所以有时需要 xpath.

I don't use id locators for example because they are all dynamic so sometimes a xpath is required.

谢谢!

推荐答案

内联框架

根据使用内联框架中的文档,内联框架是嵌入将文档转换为 HTML 文档,以便嵌入的数据显示在浏览器窗口的子窗口中.这并不意味着完全包含,两个文档是独立的,它们都被视为完整的文档,而不是将一个视为另一个的一部分.

inline frames

As per the documentation in Using inline frames, an inline frame is a construct which embeds a document into an HTML document so that embedded data is displayed inside a subwindow of the browser's window. This does not mean full inclusion and the two documents are independent, and both them are treated as complete documents, instead of treating one as part of the other.

  • 一般来说,一个 iframe 元素的形式是:

  • Generally, an iframe element is in the form of:

<iframe src="URL" more attributes>
alternative content for browsers which do not
support iframe
</iframe>

  • 支持 iframe 的浏览器在子窗口中显示 URL 引用的文档,通常带有垂直和/或水平滚动条.此类浏览器会忽略 iframe 元素的内容(即开始标记 和结束标记 </iframe> 之间的所有内容).不支持 iframe(或禁用此类支持)的浏览器则相反,即处理内容就像 <iframe...></iframe> 标签不存在.因此,尽管内容被某些浏览器忽略,但内容很重要.

  • Browsers which support iframe display the document referred to by the URL in a subwindow, typically with vertical and/or horizontal scroll bars. Such browsers ignore the content of the iframe element (i.e. everything between the start tag <iframe...> and the end tag </iframe>). Browsers which do not support iframe (or have such support disabled) does the opposite, i.e. process the content as if the <iframe...> and </iframe> tags were not there. Thus, the content matters, despite being ignored by some browsers.

    总而言之,内联框架并不意味着包含功能,尽管它有时可能用于类似的目的.

    So to summarize, inline frames do not mean an include feature, although it might sometimes serve similar purposes.

    注意,当使用内联框架时,浏览器(如果支持)会向 iframe 元素中的 URL 所引用的服务器发送请求,并在获取到请求的文档将其显示在内嵌框架内.从这个意义上说,内联框架是浏览器-服务器的联合问题,但只有浏览器需要特别识别 iframe;从服务器的角度来看,只有一个对文档的普通 HTTP 请求,它发送文档而没有(或不需要)任何关于浏览器将要做什么的想法.

    Note that, when inline frames are used, the browser (if it supports them) sends a request to the server referred to by the URL in the iframe element, and after getting the requested document displays it inside an inline frame. In this sense inline frames are a joint browser-server issue, but only the browser needs to be specifically iframe-aware; from the server's point of view, there's just a normal HTTP request for a document, and it sends the document without having (or needing) any idea on what the browser is going to do with it.

    根据最佳实践,在切换到 iframe 时,您需要按如下方式引入 WebDriverWait:

    As per the best practices while switching to an iframe you need to induce WebDriverWait as follows:

    • 切换帧名称(Java 示例代码):

    • Switch through Frame Name (Java Sample Code):

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name")));
    

  • 通过 iframe XPath(Python 示例代码)切换:

  • Switch through iframe XPath (Python Sample Code):

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ptifrmtgtframe' and @name='TargetContent']")))
    

  • 通过 iframe CssSelector 切换(C# 示例代码):

  • Switch through iframe CssSelector (C# Sample Code):

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe#twitter-widget-0")));
    

  • 您可以在以下位置找到一些相关讨论:

    You can find a couple of relevant discussions in:

    内联框架与普通框架

    这篇关于iframe下#document的处理方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆