1

(Working with Python 3 and Selenium. Trying to interact with Buffer.com's alert window after pressing "Shuffle" link.)

After pressing a button on the main browser frame, an alert pops up with two buttons. I want to press one of them.

I am trying to change the focus to this iframe that contains the alert, but I can't see it to have a name. Here's the code of said alert iframe:

<iframe src="https://js.stripe.com/v2/m/outer.html#referrer=https%3A%2F%2Fbuffer.com%2Fsignin&amp;title=Buffer&amp;url=https%3A%2F%2Fbuffer.com%2Fapp%2Fprofile%2F55e5cd556321154607cc5244%2Fbuffer%2Fqueue%2Flist&amp;muid=c7aa769e-c41c-4e86-b75f-99771764f6a5&amp;sid=26221bdc-c091-42c2-8965-8ea3e84267e2&amp;preview=false&amp;" frameborder="0" allowtransparency="true" scrolling="no" tabindex="-1" aria-hidden="true" style="width: 1px !important; height: 1px !important; position: fixed !important; visibility: hidden !important; pointer-events: none !important;"></iframe>

The element CONTAINING this iframe, in turn, has a name, but it is dynamically generated. The code of the upper level iframe containing the iframe I want is:

<iframe name="stripeXDM_default441361_provider" id="stripeXDM_default441361_provider" aria-hidden="true" src="https://js.stripe.com/v2/channel.html?stripe_xdm_e=https%3A%2F%2Fbuffer.com&amp;stripe_xdm_c=default441361&amp;stripe_xdm_p=1#__stripe_transport__" frameborder="0" style="position: absolute; top: -2000px; left: 0px;"></iframe>

The "stripeXDM_default441361_provider" is dynamic and changes every time the alert pops up.

I have tried switch_to.frame but could not get it right. I also tried to select the dynamically-generated iframe with "Xpath containing" but did not work either.

Any suggestions on how I can select this iframe with no name?

Asclepius
  • 57,944
  • 17
  • 167
  • 143
skeitel
  • 271
  • 2
  • 6
  • 17

2 Answers2

0

To select the iframe, you could use the xpath. For example, if we consider this html:

<!DOCTYPE html>
<html>
<body>


<span id="n2">

    <span id="n6">
        <a class="cn" name= "A1" >One</a>

    </span>

    <span id="n7">
         <a class="cn" name= "A2" >Two</a>
    </span>

    <span id="n8">
        <a class="cn" name= "A3" >Three</a>
    </span>
</span>


</body>
</html>

You can do this:

driver.find_element_by_xpath(("//span[@id='n2']/span[@id='n6']/a[@name='A1' and text()='One']"))

to identify the element with name 'A1' and text 'One'.

But also:

driver.find_element_by_xpath(("//span/span[1]/a[@name='A1' and text()='One']"))

So, in your case you can find the "external" iframe element and after the "internal" one (I assumed there aren't other iframe). Something like this:

//iframe/iframe

After you select the iframe you have to switch to it.

Davide Patti
  • 3,391
  • 2
  • 18
  • 20
0

It turns out I was overcomplicating things. I did not need to change focus or anything else. I just assume that it was all html and I tried to select the element directly with xpath, like so:

#do something
#press shuffle button
#once the button appears in the iframe, blurring everything else, use this line:
    button = browser.find_element_by_xpath('//*[@id="modal-buttons"]/a[2]')
    button.click()

That's what I did and worked perfectly.

skeitel
  • 271
  • 2
  • 6
  • 17