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

2024-03-29 01:43:49 发布

您现在位置:Python中文网/ 问答频道 /正文

刚刚开始使用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()")

但是有一个很长的错误,有什么提示吗?你知道吗


Tags: namedivformidfieldyourusernamepassword
1条回答
网友
1楼 · 发布于 2024-03-29 01:43:49

提交带有submit()的表单:

ele.submit()

它将找到元素的父窗体并提交它。你知道吗

或者,如果在您的情况下这不起作用,请找到Sign In按钮并单击它:

driver.find_element_by_link_text("Sign In").click()

相关问题 更多 >