pythonselenium-webdriverframesframeset

How do I switch to a frame and select an element in the frame?


I am trying to select the frame 'mainFrame'.

The page source is :

<frameset rows="89,*" frameborder="NO" border="0" framespacing="0">
    <frame name="topFrame" scrolling="NO" noresize src="inc-webpage/b-topnav.asp">
        <frameset rows="*,20" frameborder="NO" border="0" framespacing="0">
            <frameset cols="175,*" frameborder="NO" border="0" framespacing="0">
                <frame name="leftFrame" scrolling="AUTO" noresize src="inc-webpage/b-sidenav-3.asp">
                <frame name="mainFrame" src="b-default.asp">
            </frameset>
            <frame name="bottomFrame" scrolling="NO" noresize src="inc-webpage/b-footer.asp">
        </frameset>
</frameset>
<noframes>

The element I wish to select is in 'mainFrame'. Therefore my code is:

time.sleep(5)    
driver.switch_to.frame("mainFrame");
driver.find_element_by_xpath("//a[contains(text(),'I Agree')]").click()

Yes. time.sleep() is not ideal so I'm just using it for the time being.

Here is the HTML for the element I wish to select within 'mainFrame':

<input type="button" value="I Agree" 
class="btn" onmouseover="blueBtnOver(this)" onmouseout="blueBtnOut(this)" 
onclick="javascript:location.href='b-3c-pLessonBooking.asp?limit=pl'" style="background: rgb(0, 102, 204);">

Currently getting this Error:

NoSuchFrameException(frame_reference) selenium.common.exceptions.NoSuchFrameException: Message: mainFrame

I am an absolute beginner. The driver.find_element_by_xpath is probably wrong

Also why do some websites use frames//framesets while others use iframes and some don't use either?


Solution

  • Basically wait for the frame switch to it and then click the input with matching value I agree.

    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("mainFrame"))
    driver.find_element_by_xpath("//input[@value='I Agree']").click()