如何切换到Frame并使用Web驱动程序(Selenium-Java)单击弹出窗口?

时间:2012-10-04 11:09:55

标签: java selenium automated-tests ui-automation selenium-webdriver

<iframe height="0" frameborder="0" width="500" scrolling="no" style="position: absolute; top: 185.5px; left: 460.5px; height: 357px; width: 500px; z-index: 10002; border: medium none; background-color: rgb(255, 255, 255); opacity: 0;" src="/foresee/shim.gif" alt="Survey Invitation Helper Window - Please Ignore">
<html>
<head>
<meta name="viewport" content="width=device-width; height=device-height;">
<style>
<title>shim.gif (GIF Image, 1&nbsp;×&nbsp;1 pixels)</title>
</head>
<body>
<img src="https://www3-qan.tivo.com/foresee/shim.gif" alt="https://www3-qan.tivo.com/foresee/shim.gif">
</body>
</html>
</iframe>
<div class="fsrwin" style="position: absolute; width: 500px; z-index: 10005; left: 460.5px; top: 185.5px; opacity: 1;">
<div class="innerwin">
<div class="fsr_top">
<div class="fsr_middle">
<div class="fsr_closeBody fsr_closeButtons">
<button id="decline" class="fsr_closeSticky fsr_button fsr_decline">No thanks</button>
<button id="accept" class="fsr_closeSticky fsr_button fsr_accept">Yes, I'll give feedback</button>
</div>

我知道我应该切换到帧以便对帧中的任何操作进行操作。

我的问题是:

  • 如何切换到相框? (我无法找到合适的人选 在switch语句中使用的定位器(driver.switchto()。frame(“WHAT 应该在这里“)))?

  • 当我导航到新的时,有没有办法检查这个框架是否存在 每次页面?

非常感谢帮助...... 麦克

2 个答案:

答案 0 :(得分:4)

如果框架是页面上唯一的框架,那么您可以使用

driver.switch.frame(0); //不是一个好方法

您可以使用xpath或其他任何内容来识别您的iframe,然后使用下面的

driver.switch.frame(driver.findElementByXpath("//iframe[contains(@src,'forsee')]"))

元素标识应该是唯一的..上面给出的xpath只是一种方式..但它可能不是唯一的..取决于整个html。

希望有所帮助..

答案 1 :(得分:0)

通过selenium ide记录您的测试,并根据您的意愿将其导出到selenium rc junit或testng。

会有像 selenium.selectFrame(frameName)这样的行。在 driver.switchToFrame(frameName)中使用相同的框架名称。