python - 通过 Selenium 提交表单只会带回原始页面

标签 python forms selenium python-3.x webautomation

我正在尝试使用 Selenium 登录网站。网址是http://projecteuler.net/login .

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('http://projecteuler.net/login')
username = browser.find_element_by_id('username')
username.send_keys(USERNAME_HERE)
password = browser.find_element_by_name('password')
password.send_keys(PASSWORD_HERE)

browser.find_element_by_name("login").submit()

直到最后一条语句,程序都正常工作。我尝试省略最后一条语句并手动登录并且它起作用了。但是当我添加最后一条语句并运行程序时,它似乎只是重新加载了同一个页面,减去了我通过程序放置的信息。

所以只有提交才有问题。我查看了源代码并确认是否有该名称的其他元素,但没有名称为“登录”的其他元素。那我在这里弄错了什么?我还需要处理其他事情吗?

奇怪的事情发生了。当我通过代码完成表单提交并尝试在 Google Chrome 33.0.1750.154 m 中查看源代码时,我得到以下信息。 enter image description here

最佳答案

尝试使用 click() 而不是 submit()

Submit is particularly useful for forms without submit buttons, e.g. single-input “Search” forms.

来源:- http://selenium.googlecode.com/git/docs/api/py/selenium/selenium.selenium.html?highlight=submit#selenium.selenium.selenium.submit

在你的例子中有一个提交按钮,最好直接点击它。

关于python - 通过 Selenium 提交表单只会带回原始页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22541636/

相关文章:

python - 在django中进行单元测试时出现"auth_user does not exist"

python - 从 fastapi 返回多个文件

Python - urllib.request 和 Flask

selenium - InternetExplorerDriver getPageSource() 返回的字符串与 FirefoxDriver 不同

python - 将双值 numpy 数组屏蔽为 0-1 二进制数组

html - 将具有特定 ID 的 HTML 表单封装在新标签中

php - 根据选择选项标签更改输入值

php - 在 Drupal 7 中获取提交的帖子值

python - 我如何解决此错误 'webelement does not support indexing"[webdriver][python]

javascript - Selenium:弹出窗口未显示在窗口句柄中