I'm trying to automate a simple page that has a colorbox that loads an iFrame inside. Whenever I record a test in Selenium it captures the iframe just fine. But then when I 开发者_如何学JAVAre-run the test it cannot grab the iframe inside. I looked into it and you'll see this code:
Command: selectFrame Target: iframe_1313186641607 Value: (blank)
I go to Firefox and inspect element and I see:
<div>
<div id="cboxMiddleLeft" style="float: left; height: 558px;"></div>
<div id="cboxContent" style="float: left; width: 698px; height: 558px;">
<div id="cboxLoadedContent" style="display: block; width: 698px; overflow: auto; height: 530px;">
<iframe id="cboxIframe" frameborder="0" src="SelectCourseOptions.aspx?courseno=111&subject=ACC&term=Fall 2010" name="iframe_1313186641607">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<body style="background-color: White">
<form id="form1" action="SelectCourseOptions.aspx?courseno=111&subject=ACC&term=Fall+2010" method="post" name="form1">
<div>
So you can see it's matching on the "name" of the iframe. Then, I'll go re-run the test and it can't select the iframe, I'll inspect element again and see this:
<div>
<div id="cboxMiddleLeft" style="float: left; height: 558px;"></div>
<div id="cboxContent" style="float: left; width: 698px; height: 558px;">
<div id="cboxLoadedContent" style="display: block; width: 698px; overflow: auto; height: 530px;">
<iframe id="cboxIframe" frameborder="0" src="SelectCourseOptions.aspx?courseno=111&subject=ACC&term=Fall 2010" name="iframe_1313186725931">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<body style="background-color: White">
<form id="form1" action="SelectCourseOptions.aspx?courseno=111&subject=ACC&term=Fall+2010" method="post" name="form1">
<div>
Now you can see that the iframe name:
name="iframe_1313186725931"
That is different from the first time when I recorded the test:
name="iframe_1313186641607"
Just for fun I closed the colorbox and re-opened and the name changed again. How can I work around this? It seems colorbox just generates a dynamic name every time so I can't select it in Selenium?
Thanks for your help.
I figured out how to do this, I was just being a Selenium n00b. I needed to select the colorbox by id instead of by the name tag. Like this:
Command: selectFrame Target: id=cboxIframe Value: (blank)
That works!
In case others are having a similar issue with dynamic iframes, note i've used php selenium webdriver "facebook":
$iFrame = $webDriver->findElement(WebDriverBy::xpath('//iframe[@id="cboxIframe"]'));
or
$iFrame = $webDriver>findElement(WebDriverBy::xpath('//iframe[@class="cboxIframe"]'));
or
$iFrame = $webDriver->findElement(WebDriverBy::xpath('//iframe[@name="cboxIframe"]'));
And finally switch focus to iFrame
$webDriver->switchTo()->frame($iFrame);
精彩评论