使用selenium和python时设置发送密钥的正确方法是什么

时间:2017-04-28 13:06:00

标签: python selenium selenium-webdriver automation webdriver

我是新的python(编程)。目前正在开发一个使用selenium和python填充表单的项目。

所以一切似乎都没问题。我在我的窗口上成功安装了python 3.6,pylenm和chrome驱动器上的selenium。

我尝试打开网站并使用脚本填写表单。该脚本打开了网站,但无法将文本放在表单中。

我一直在浏览堆栈溢出和谷歌搜索几个小时,任何事情都不起作用。

它显示有关sendkeys的错误。

以下是完整代码

from selenium import webdriver

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

# Replace below path with the absolute path
# to chromedriver in your computer
driver = webdriver.Chrome('C:\\Users\public\chromedriver.exe')


# Go to your page url
driver.get('https://www.website.com/')

# Get button you are going to click by its id ( also you could us find_element_by_css_selector to get element by css selector)
button_element = driver.find_element_by_class_name('signup_email_link')
button_element.click()

input_element = driver.find_element_by_name("first_name")
input_element.sendkeys(ram)

以下是错误代码

C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe "C:/Users/user/PycharmProjects/My Projects/My scripts automate.py"
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/My Projects/My scripts automate.py", line 22, in <module>
    input_element.sendkeys(ram)
AttributeError: 'WebElement' object has no attribute 'sendkeys'

Process finished with exit code 1

我甚至尝试用web_element替换input_element但错误连续。

我在pycharm上使用最新的Python 3.6版本。

2 个答案:

答案 0 :(得分:2)

使用

input_element.send_keys(ram)

而不是

input_element.sendkeys(ram)

答案 1 :(得分:2)

应为input_element.send_keys(ram)

List of methods here.