質問

I am new to Selenium. The webpages, I have to automate, have iframes for some pages and not for the rest. How to identify whether there is iframe or not so as for that particular page in java only and not from the firebug. If the page has iframe use webdriver.switchTo().defaultContent(); else check for xpath directly

役に立ちましたか?

解決 2

Open the page in browser and see HTML code for the page using Firebug, Developer Tools etc. If HTML has tag iframe, that means your page has iframe and hence you'll have to use driver.switchTo.frame(frame); to interact with elements within the frame.

If you want to check with Java code, do following:

driver.getPageSource().contains("iframe"); //this will return true if there is iframe

他のヒント

Try to find iframe(s) on page:

List<WebElement> iframes = webDriver.findElements(By.tagName("iframe"));

There will all found iframes as list of WebElement.

If iframes.size() > 0 it means that iframes found on page. And vice versa.

So if iframes found you can work with them as with webelements. In example you can switch to any iframe from the list do something inside it and get back.

You can use By.tagName method to find iframes

int count = driver.findElements(By.tagName("iframe")).size() ;
if(count == 0){
   // No frames          
}else{
   // Frames present         
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top