我正在尝试使用Python中的Selenium登录网站。我的代码如下。我能够成功找到(by_xpath)用户名字段并打印其属性。但是,我无法send_keys或以任何方式与它进行交互。此外,我甚至无法选择密码字段,即使各种xpath实用程序告诉我我正在使用该字段的正确地址。
我想输入我的用户名,密码并点击提交。
from selenium import webdriver
from selenium.webdriver.common import action_chains, keys
import time
driver = webdriver.Firefox()
driver.get("http://games.espn.go.com/ffl/signin?redir=http%3A%2F%2Fgames.espn.go.com%2Fffl%2Fleaguerosters%3FleagueId%3D1111554")
time.sleep(10)
driver.find_element_by_xpath("//input[@type='text']").send_keys("JimH")
driver.find_element_by_xpath("//input[@type='password']").clear()
driver.find_element_by_xpath("//input[@type='password']").send_keys("N0tMyR3@1P@55w0rd")
driver.find_element_by_xpath("//button[@type='submit']").click()
您可以在我的代码中看到我尝试访问的网站是this。
我在这个论坛上广泛搜索了一个解决方案:
我从上面的代码收到的例外情况是粘贴在这里:
/usr/bin/python2.7 /home/ff/PycharmProjects/Test/Test_action_chain.py
Traceback (most recent call last):
File "/home/ff/PycharmProjects/Test/Test_action_chain.py", line 8, in <module>
driver.find_element_by_xpath("//input[@type='text']").send_keys("JimH")
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 328, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpOFHnLq/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpOFHnLq/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpOFHnLq/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpOFHnLq/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpOFHnLq/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
答案 0 :(得分:0)
您要填写的表单在iframe
范围内。要使硒在iframe
中工作,您需要使用switch_to_frame()
功能切换到您希望使用的特定帧。您提到的语法不正确。试试下面给出的代码。
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get(link)
driver.switch_to.frame(driver.find_element_by_id("disneyid-iframe"))
driver.find_element_by_xpath("//input[@type='text']").send_keys("JimH")
...
当你想离开画面时,请使用:
driver.switchTo().defaultContent();
提示:使用waits代替time.sleep(10)
。