Jen Jen - 4 months ago 24
Ruby Question

Seleium/Ruby - NoSuchElement when trying to access an item in a modal popup

What I'm attempting:

  1. Click on a the profile picture of a user that I previously created:

  2. Click on and make a selection from a drop down in the pop up modal:

selenium IDE
picks up the steps as:

  1. click >

  2. selectWindow >
    (NOTE that the modal number changes and increases exponentially, if i run these steps again the next time around it's modal4)

  3. click >
    click >

This playback in the ide works without issue.

My code:

@driver.find_element(:id, "patientPhoto").click
wait_for { displayed?(:id, "ext-gen31") }
@driver.find_element(:id, "ext-gen31").click
@driver.find_element(:xpath, "//div[@id='ext-gen179']/div[2]").click

In other places/workflows I have to do the switch to default content then switch to frame trick:


but no combination of that is working here.

Other things I've tried:

1. Switch to the last window like this: @driver.switch_to.window(@driver.window_handles.last) # this doesn't fail but the step after this (finding the drop down) fails
2. @driver.switch_to.frame('modal4') then attempt to access the items in the modal # this fails i think because the modal number keeps changing

Here's the page coding -

errors I get vary based on what I'm attempting but it's either timeout or cannot find element.


If there is an iframe with dynamic id, try to use following line of code to switch to it:

@driver.switch_to.frame(@driver.find_element(:xpath, "//iframe[contains(@id, 'modal')]"))