【问题标题】:Unable to Send Keys To Website with Selenium Python on Centos in Firefox无法在 Firefox 的 Centos 上使用 Selenium Python 向网站发送密钥
【发布时间】:2015-10-26 11:40:05
【问题描述】:

我正在尝试使用 Python 中的 Selenium 登录网站。我的代码如下。我能够成功找到(by_xpath)用户名字段并打印其属性。但是,我无法向它发送密钥或以任何方式与之交互。此外,我什至无法选择密码字段,尽管各种 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)

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    您要填写的表格在iframe 内。要使 selenium 在 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)

    【讨论】:

    • 杰森,你的建议奏效了!感谢您的及时和彻底的回复!您能否简要解释一下我如何知道我正在寻找的元素位于不同的框架上?
    • 使用浏览器的开发者控制台查看HTML页面的元素树。对于 Chrome,您可以关注此link。对于 Firefox,请关注此link
    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多