0

I'm completely new to selenium and javascript. The webpage that I'm working on uses javascript to do certain authentication and construct the url for a frame.

The code looks something like:

<iframe id="main_contents"></iframe>
<script type="text/javascript">
var baseUrl = 'http://xxxxxxxxx.com/';
function init() {
    gadgets.io.makeRequest(baseUrl+"auth/", function(obj) {
        var iframe = document.getElementById("main_contents");
        if (obj.data.status == 'OK') {
            iframe.src = baseUrl+"?xx="+obj.data.xx;
            ...
        }
        ...
    }, params);
}

I have tried to directly locate the frame with find_element_by_tag_name but of course its src is empty. I have no idea how to switch to this frame. Could anyone suggest? Thank you very much!!

1
  • Problem has been solved Commented Oct 22, 2016 at 8:26

1 Answer 1

1

You can switch to this iframe using their id attribute value as well as below :-

driver.switch_to_frame("main_contents")

Or if this iframe takes time to load on the DOM, you should try to switch using Explicit Waits with WebDriverWait to wait until iframe available to switch as well as below :-

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(("main_contents")))
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your comment, but I have tried that, it doesn't work. There was no error but the session is not switch to the frame. Ex, I tested
frame = browser.find_element_by_id("main_contents") print (frame.get_attribute("src")) there is no output
@Seraph if there is no error, how could you know it didn't switch to iframe?? Did you find some element after switching inside this iframe?? And do you want to switch iframe element or want to get it as src attribute value??
And make sure src attribute value is present in iframe as well
Thank you so much! I finally figured it out that the session will first jump to an invisible frame A and redirect to frame B immediately. A and B have very similar src and I was not aware of the existence of frame A. I thought selenium is in frame B but actually it's stuck on frame A.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.