无法在Firefox中的Centos上使用Selenium Python将密钥发送到网站 [英] Unable to Send Keys To Website with Selenium Python on Centos in Firefox

查看:108
本文介绍了无法在Firefox中的Centos上使用Selenium Python将密钥发送到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python使用Selenium登录一个网站.我的代码如下.我能够成功找到(by_xpath)用户名字段并打印其属性.但是,我无法向它发送send_keys或以任何方式与其进行交互.此外,即使各种xpath实用程序告诉我我在该字段中使用了正确的地址,我什至无法选择密码字段.

I am trying to log into a website with Selenium in Python. My code is below. I am able to sucessfully find (by_xpath) the username field and print its attributes. However, I am unable to send_keys to it or interact with it in any way. Further, I am unable to even select the password field, even though various xpath utilities tell me I am using the right address for the field.

我想输入我的用户名,密码,然后单击提交.

I would like to input my username, password and click submit.

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()

您可以在我的代码中看到我要访问的站点是

You can see in my code that the site that I am trying to access is this.

我已经在这个论坛上广泛搜索了解决方案:

I have searched this forum extensively for a solution:

  • This didn't help because I wasn't sure how to send keys, all this idd was click. Also it didn't provide descriptive error messages: Selenium Element not visible exception
  • When I tried this one I got an exception that the box I was trying to select was outside the scroll window, which was not true: Unable to select radio button with selenium in Python
  • Another post told me to select an frame which i was unable to do. (select_frame("disneyid-iframe") errored out on me)

我从上面的代码中收到的异常粘贴在这里:

The exception that I receive from my code above is pasted here:

/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)

推荐答案

您要填写的表单位于iframe之内.要使硒在iframe中工作,您需要使用 switch_to_frame() 函数.您提到的语法不正确.尝试下面给出的代码.

The form which you are trying to fill is within an iframe. To make selenium work in an iframe you need to switch to the specific frame you wish to work in using switch_to_frame() function. The syntax which you mentioned is incorrect. Try the code given below.

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")
...

当您想脱离框架时,请使用以下方法:

When you want to get out of the frame, use this:

driver.switchTo().defaultContent();

提示:使用 waits 代替time.sleep(10).

这篇关于无法在Firefox中的Centos上使用Selenium Python将密钥发送到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆