使用Python在Selenium中选择带索引的框架集

时间:2016-03-24 23:10:24

标签: python selenium selenium-webdriver

我目前正处于使用python学习selenium的阶段。

以下是我试图运行的代码:

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)

但是我无法这样做并最终出错。 以下是我看到的错误

/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/fxdriver@googlecode.com/components/driver-component.js:10783)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpbrHNVJ/extensions/fxdriver@googlecode.com/components/command-processor.js:12614)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpbrHNVJ/extensions/fxdriver@googlecode.com/components/command-processor.js:12619)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpbrHNVJ/extensions/fxdriver@googlecode.com/components/command-processor.js:12561)

以下是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>

以下是我的疑惑:

  1. 什么是Java的python替代品 browser.swichTo()。Frame(int arg0)?

  2. 将webelements存储在变量中并使用索引检索所需值的方法是正确的方法吗?

  3. 请咨询, 谢谢!

1 个答案:

答案 0 :(得分:3)

  

什么是Java的browser.swichTo()的python替代.Frame(int arg0)?

这与Python非常相似:

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

将webelements存储在变量中并使用索引检索所需值的方法是正确的方法吗?

是的,如果elems内的元素是frame元素,它可能会起作用。

在这种情况下,您可以执行以下操作:

browser.switch_to.frame(0)

或:

browser.switch_to.frame("login_page")