问题描述
对于我现在正在测试的门户,我遇到了无法创建任何 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>
标签不存在.因此,尽管被某些浏览器忽略,但内容很重要.
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:
切换Frame Name(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#示例代码):
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe#twitter-widget-0")));
您可以在以下位置找到一些相关讨论:
You can find a couple of relevant discussions in:
- Python:如何选择一个 html 元素,无论它在 selenium 中的哪个框架中?
- Java:是否可以在 Selenium Webdriver Java 中不使用 driver.switchTo().frame(frameName") 切换到框架中的元素?李>
- C#:如何在定位元素之前等待框架加载?
内嵌帧与普通帧
这篇关于iframe下处理#document的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!