Search code examples
pythonseleniumswitch-statementframes

Cannot switchTo frames -- keep getting AttributeError


my first time posting. My apologies in advance for any missteps.

I am writing in Python, using Selenium, trying to scrape some information from some webpages.

I can not seem to solve this puzzle after two days of searching-trying-searching.

My issue is this; while trying to sign in to a website w/Selenium/Python, I am unable to 'find element' to log in. After much searching, I realize that I may need to switch frames to find the element. I've tried the switchTo (or switch_to) command many different ways--and I keep getting the message

"Attribute error: SwitchTo instance has no call method".

I will post the most recent attempt (please let me know if I can provide any further information):

In selenium:

waitUntilReady(browser)  
browser.switch_to().frame(browser.findElement(By.ID("iframe[id='credentials']")))
elem = WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.NAME, "Ecom_User_ID")))
elem = browser.find_element_by_name("Ecom_User_ID")
elem.send_keys("frustrated")

What gets returned (in terminal):

File "someproj.py", line 56, in browser.switch_to().frame(browser.findElement(By.ID("iframe[id='credentials']"))) AttributeError: SwitchTo instance has no call method

html form website:

 <!DOCTYPE html>

    <html lang="en" webdriver="true">
        <head></head>
        <body onload="onloadhandler()">
            <div class="m-header"></div>
            <div class="container">
                <div class="header"></div>
                <div class="ten columns">
                    <h2></h2>
                    <iframe id="loginsubtab" height="375" frameborder="0" width="100%" src="/nidp/jsp/content.jsp?sid=0&id=289&sid=0" scrolling="no">
                        #document
                            <!DOCTYPE html>
                            <html lang="en" webdriver="true">
                                <head></head>
                                <body onload="onloadhandler('selectedCard')">
                                    <div id="content">
                                        <table border="0" width="100%">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        <iframe id="credentials" height="375" frameborder="0" width="100%" src="/nidp/saml2/sso?id=289&sid=0&option=credential&sid=0" scrolling="no">
                                                            #document

Many thanks for any ideas or guidance!!!


Solution

  • You are using wrong selector. The selector you are using is cssSelector but NOT id

    waitUntilReady(browser)  
    # or use id as follows
    # browser.switch_to.frame(browser.findElement(By.ID, 'credentials'))
    #browser.switch_to.frame(browser.find_element_by_id('credentials'))
    browser.switch_to.frame(browser.findElement(By.CSS_SELECTOR, "iframe[id='credentials']"))
    elem = WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.NAME, "Ecom_User_ID")))
    elem = browser.find_element_by_name("Ecom_User_ID")
    elem.send_keys("frustrated")