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

标签 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()")

但是出现了一个很长的错误,有什么指示吗?

最佳答案

使用submit()提交表单:

ele.submit()

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

或者,如果这不适用于您的情况,请找到登录按钮并单击它:

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

关于python - 如何使用selenium填写表单后执行javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31842646/

相关文章:

python - 包含要使用 python 保存在 Excel 中的值列表的变量

ruby - 有没有办法使用 Selenium 和 Watir 确定某个元素在 Chrome 浏览器中是否可点击?

java - 如何使用Selenium选择 “style”的值

java - WebElement.findElement 未找到子元素

java - Jenkins 用户未启用 Safari WebDriver 扩展

python - Pycharm (@property) 和 (@x.setter) 自动生成

python - 不那么丑陋但可靠的方法来找到嵌入在字符串中的浮点值而不需要填充?

python - 在 Python 中使用逗号、连接和字符串格式化程序之间的区别

javascript - 无法在 javascript 中使用 webdriver 打开 chrome

java - 将 Java 变量传递给 jQuery 函数的 JavascriptExecutor 参数