問題描述
對于我現在正在測試的門戶,我遇到了無法創建任何 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,"http://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的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!