有沒有什麼方法可以在硒中識別以下內容?使用硒識別頁面中的iframe數量?
Number of iframes in a page
Attributes/Details of the current iframe
有沒有什麼方法可以在硒中識別以下內容?使用硒識別頁面中的iframe數量?
Number of iframes in a page
Attributes/Details of the current iframe
driver.findElements(By.xpath("//iframe")).size();
獲取當前幀我建議您切換使用WebElement
對象和switchTo
它,然後得到像你通常做屬性的細節,與getAttribute
UPD
事實上,是的,首先會給出當前上下文中iframe的數量。如果你不想這樣做遞歸,但想快速和工作(髒)解決方案 - 剛剛獲得頁面的源代碼,並找到"<iframe"
串
這裏的例子,你可以如何對待它的全部夾雜物:
WebDriver driver = new FirefoxDriver();
driver.get("http://the-internet.herokuapp.com/iframe");
// find all your iframes
List<WebElement> iframes = driver.findElements(By.xpath("//iframe"));
// print your number of frames
System.out.println(iframes.size());
// you can reach each frame on your site
for (WebElement iframe : iframes) {
// switch to every frame
driver.switchTo().frame(iframe);
// now within the frame you can navigate like you are used to
System.out.println(driver.findElement(By.id("tinymce")).getText());
}
至於其他的答案都表示,你可以使用識別當前的重點方面幀數量:
driver.findElements(By.xpath("//iframe")).size();
但是這並不確定是肛門的孩子任何幀其他框架。爲此,您需要先切換到該父框架。
檢索屬性,如姓名或ID爲當前聚焦框,你可以使用JavascriptExecutor像這樣:
String currentFrameName = (String)((JavascriptExecutor) driver).executeScript("return window.frameElement.name");
這是我相信的最佳答案。 –
我在使用上面的代碼時收到以下錯誤: 「無法讀取空值的屬性'名稱' 有沒有我在這裏失蹤的東西? – justcurious
當您嘗試執行「return window.frameElement.name」時,您是否處於頁面的頂級上下文中?如果您關注頁面的頂級內容,則「window.frameElement」將返回null。 –
謝謝大家!與size()方法一起,使用switchTo()方法導航到框架。最初我很難用java代碼來識別當前幀(我想知道是否有!)。通過chrome中的Inspect元素的幫助,確定了框架名稱,現在我能夠識別該框架中的元素。 – justcurious