-2

I want to press buttons using selenium, i faced an issue yesterday where i couldn't and simply turned out that my button is inside an iframe circling the entire webpage. this fixed it:

from time import sleep
driver.switch_to.frame(driver.find_element_by_id("includedPage"))
sleep(10)
element = driver.find_element_by_xpath("//input[contains(@id, 'workbenchLst:j_id_id507') and @value='Add']")
element.click()

after clicking on it i wanted to do same with other buttons on the page after but couldn't i just keep getting Unable to locate element for the iframe and all other elements in the page( that page has an iframe too inside a td like this):

<td id="rightTdId">                                 
                                    <iframe id="includedPage" style="width:100%;" onload="reRenderReportLOV();;tickleSession('pageNavigation:sessionLink');disableContextMenu();showHidPanelMenu('hide','leftMenuj_id_1');" name="includedPage" src=".." marginheight="0" marginwidth="0" height="531" frameborder="0"></iframe>
                                </td>

how can i get my buttons in that page and disable iframe's effect affect after locating it?

Steve
  • 55
  • 1
  • 5
  • The iframe in the first page circles it all(all the content is in it), the page after shares the same exact iframe id and url but it's a different content all together so i'd say no not same iframe . – Steve Feb 05 '19 at 07:01

2 Answers2

1

If both the buttons belongs to different different iframes, you will have to switch the focus of web driver to parent and then again switch it to 2nd iframe. Something like this :

driver.switch_to.frame(driver.find_element_by_id("includedPage"))
sleep(10)
element = driver.find_element_by_xpath("//input[contains(@id, 'workbenchLst:j_id_id507') and @value='Add']")
element.click()

driver.switch_to.default_content()  

driver.switch_to.frame(driver.find_element_by_id("id of different iframe"))

element2 = driver.find_element_by_xpath(" xpath of 2nd button")
element2.click()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

When you have clicked on the iframe and you need to come back to the prev frame or the default frame, you should use driver.switch_to.default_content() and then you can click on the button on the default content.

This would switch the context to the default frame and you would be able to again operate on it.

Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • Can you add that in my code shared above to be sure at which stage to add it, thanks. – Steve Feb 05 '19 at 07:00
  • Steve you can add this before you finding the element i.e. button which you wants to click – akshay patil Feb 05 '19 at 07:01
  • You can do `driver.switch_to.frame(driver.find_element_by_id("includedPage"))` and then click on the element on the iframe and can use `element.click()` and then if you want to switch to the default content, you can use `driver.switch_to.default_content()` and then can click on the other element on the default content. – Sameer Arora Feb 05 '19 at 07:04