如何在使用selenium填写表单后执行javascript?

时间:2015-08-05 20:55:12

标签: python selenium selenium-webdriver

刚开始使用selenium,这是页面的HTML:

<div id="signInForm">
    <form action="/cgi-bin/VmLoginCgi" method="POST" name="signIn1" id="signIn">
        <h2 id="loginTitle">Sign in to view and change your settings</h2>
        <div class="field formField noHint username clearfix">
            <label for="username"></label>
        </div>
        <div style="position:relative;" class="field formField noHint password clearfix"><label for="password">Settings Password</label><input type="password" autocomplete="off" value="" maxlength="15" class="name required onefiftyPX inactive" name="jgwhnZLOXn" id="password" onkeypress="handleKeyPress(event)"></div>

        <div>
            <a href="javascript:SignIn()" class="newCta ctaLarge ctaPrimaryLarge floatLeft">Sign In</a>
        </div>
    </form>
    <p>
        <span style="font-weight:bold;">Don't know your password?</span><br>You'll find your default password on the bottom of your Super Hub.
    </p>
</div>

我通过以下方式填写文本框:

ele = browser.find_element_by_id("password")
ele.send_keys("supersecretpassword")

但是无法通过执行javascript来提交实际表单。我试过了:

 browser.execute_script("SignIn()")

但是得到一个很长的错误,任何指针?

1 个答案:

答案 0 :(得分:1)

使用RequestedTheme提交表单:

submit()

它会找到元素的父表单并提交它。

或者,如果您的情况不适用,请找到ele.submit() 按钮并单击它:

Sign In