1

Im currently in the phase of learning selenium with python.

Following is the code im trying to run:

browser=webdriver.Firefox()
browser.maximize_window()
browser.get('https://netbanking.hdfcbank.com')
#print browser.page_source

elems=browser.find_elements_by_tag_name('frameset')
browser.switch_to_frame(elems[1])
browser.find_element(By.CSS_SELECTOR,"input[class='input_password']").send_keys('123456789'+Keys.ENTER)

However Im unable to do so and ends up in an error. Following is the error i see

/usr/bin/python2.7 /home/abhishek/PycharmProjects/Selenium/01_Selenium.py
Traceback (most recent call last):
  File "/home/abhishek/PycharmProjects/Selenium/01_Selenium.py", line 115, in <module>
    browser.switch_to_frame(elems[1])
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 537, in switch_to_frame
    self._switch_to.frame(frame_reference)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/switch_to.py", line 67, in frame
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: Element is not a frame element: FRAMESET
Stacktrace:
    at FirefoxDriver.prototype.switchToFrame (file:///tmp/tmpbrHNVJ/extensions/[email protected]/components/driver-component.js:10783)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpbrHNVJ/extensions/[email protected]/components/command-processor.js:12614)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpbrHNVJ/extensions/[email protected]/components/command-processor.js:12619)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpbrHNVJ/extensions/[email protected]/components/command-processor.js:12561)

Following is the pagesource:

/usr/bin/python2.7 /home/abhishek/PycharmProjects/Selenium/01_Selenium.py
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>Welcome to HDFC Bank NetBanking</title>
<script language="javascript">
    var daemon          = 'NETBANKING';
    var p_remoteaddress = '';
    var RsaAuthReq      = '';

    var l_path = window.location.pathname;

    if(l_path == undefined || l_path == '' || l_path.indexOf("/netbanking") &lt; 0){
        window.location.href = window.location.protocol + "//" + window.location.host +"/netbanking";
    }

</script>
</head>
    <frameset cols="*" rows="*" framespacing="0" frameborder="O" border="false">
        <frameset cols="*" rows="*,30" framespacing="0" frameborder="O" border="false">
            <frame scrolling="yes" noresize="true" src="RSLogin.html?v=2" name="login_page" marginheight="0" marginwidth="0" />
            <frame scrolling="no" noresize="true" src="footer.html" name="footer" marginheight="0" marginwidth="0" />
        </frameset>
    </frameset>

</html>

Following are my doubts:

  1. What is the python alternative to Java's browser.swichTo().Frame(int arg0) ?

  2. Is the way of storing the webelements in a variable and using indexes to retrieve the required value a correct approach?

Kindly advice, Thanks!

2
  • what error does it give you? also, use browser.find_element_by_css_selector instead of By.CSS_SELECTOR Commented Mar 24, 2016 at 23:18
  • @n1c9 Ive added the error in the main description. Commented Mar 24, 2016 at 23:28

1 Answer 1

3

What is the python alternative to Java's browser.swichTo().Frame(int arg0) ?

This is quite similar in Python:

number = 0  # first frame
browser.switch_to.frame(number)

Is the way of storing the webelements in a variable and using indexes to retrieve the required value a correct approach?

Yes, it could've worked in case the elements inside elems would be the frame elements.

In this case, you can do the following:

browser.switch_to.frame(0)

or:

browser.switch_to.frame("login_page")
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @alecxe ! Following doubts: 1. Can you explain why 0th index worked over here when as per the page source the login page falls under the second(1st index) frameset. 2. can any unique identifier be used as a frame reference in switch_to.frame?
@abhisheknair the frame containing the login field is the first frame element on the page and indexing starts with 0..you can use 4 things in browser.switch_to.frame(...): name, id, index and the WebElement instance you've previously located. The last one - is what you were trying to do, but instead of framesets, you should've located the frame elements..hope that makes things clearer..
Thanks again @alecxe !! :)

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.