简体   繁体   中英

Unable to locate and switch to iframe using Eclipse with Selenium webdriver Java

When I click on a link a popup opens where I can fill some fields. However, this popup contains an Iframe that contains the fields I want to use in my test. The problem is that I can't seem to switch to the iFrame in order to reach the fields.

I've tried the following first but this results in a (org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/iframe"}):

driver.switchTo().frame(driver.findElement(By.xpath("/html/body/iframe")));

I then tried the following, this seems to pass the iFrame but resulted in (org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for element to be clickable: By.id: //div[@class='actionbutton-content unselectable']) and (NoSuchElementException: Unable to locate element: {"method":"id","selector":"//div[@class='actionbutton-content unselectable']"}):

driver.switchTo().frame(0);
wait.until(ExpectedConditions.elementToBeClickable(By.id("//div[@class='actionbutton-content unselectable']")));

Here is the first section of the page source:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Page title</title>
    <script src="scripts/JQuery/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
    script here....
    </script>
</head>

<body MS_POSITIONING="GridLayout" leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
    <input type="hidden" id="newxObjectID"> <input type="hidden" id="newxObjectName"/>
    <iframe height="100%" width="100%" frameborder="0" scrolling="no" src="fxeditobject.aspx?function=showobjectfields&key=0x3928x0x60937x&viewid=&callerwindow=true&fieldDefId=0&defid=3928"  >
    #document
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html webdriver="true">
    <head></head>
    <body id="PageBody" onload="" etc. etc. etc. >
**Fields I'm looking for are somewhere in this area**
    </iframe>
</body>

Have you tried a slightly different xpath? -> double slash "//html/body/iframe"

driver.switchTo().frame(driver.findElement(By.xpath("//html/body/iframe")));

if that doesn't work, maybe the iframe is not directly located under your body tag (did you post the complete html or a snippet?), then you go for another double slash in front of iframe:

driver.switchTo().frame(driver.findElement(By.xpath("//html/body//iframe")));

As for your second problem:

If you provide an xpath-expression, you should also search By.xpath (you searched By.id):

driver.switchTo().frame(0);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='actionbutton-content unselectable']")));

Not sure if this is the way to go but I seem to have found a solution or at the very least a workaround. Right after the line of code that clicks a link that opens the new window I've placed the code below:

Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
//iterate through your windows
while (it.hasNext()){
String parent = it.next();
String newwin = it.next();
driver.switchTo().window(newwin);

driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.xpath("//html/body/iframe")));

As far as I understand the code (I've assembled it using google :-)) the first bit of the code is to switch to the window that has just opened. I then switch to default content to make sure I'm not unwillingly in any frame of any kind. I then switch to the iFrame that holds the fields I need to fill or test. Now allthough this work, it seems to take 10 mninutes and I'm not sure why this is the case? Is there a way to figure out which steps takes so long and why?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM