Search code examples
javaseleniumautomated-testsui-automationselenium-webdriver

How do I switch to a Frame and click on the pop up using web driver (Selenium - Java)?


<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>

I understand that I should switch to frame in order to do to any operations in the frame.

My problem in there is:

  • How do I switch to frame? (I am not able to figure out the right locator to use in the switch statement (driver.switchto().frame("WHAT SHOULD GO IN HERE")))?

  • Is there a way to check if this frame exists when i navigate to a new page every time?

Thanks a ton for the help.... Mike


Solution

  • If the frame is the only frame on the page then you can go with

    driver.switch.frame(0); //not a good way though

    or

    you can use xpath or anything to identify your iframe and then use that as below

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

    The element identification should be unique..the xpath given above is just one way..but it might not be unique..depends on your entire html.

    Hope that helps..