Selenium“nonetype”对象没有“send_keys”属性。

3
title='this is the title'

我希望用Python/Selenium定位网页中的这一行:
<input id="subject" name="subject" maxlength="50" value="" class="nude error" type="text">

我在Debian系统中使用python-selenium库,以下是我的代码:

title = driver.find_element_by_id("subject").clear()
title.send_keys(title) 

我收到了以下错误信息:
Traceback (most recent call last):

  File "./basic0", line 49, in <module>
titre.send_keys(title)

AttributeError: 'NoneType' object has no attribute 'send_keys'

注意:当脚本因此错误停止时,鼠标光标位于网页中的正确位置;但我找不到一种方法来使用send_keys填写输入框。

我也尝试过:

title = driver.find_element_by_xpath("div[contains(text(),'subject')]")

title = driver.find_element_by_xpath("//form[input/@id='subject']")

title = driver.find_element_by_xpath("//form[input[@name='subject']")

但是它不起作用;此外,鼠标指针也不在正确的位置。

然后我尝试了一个更新版本的selenium:

我完全清除了Debian下的python-selenium软件包(它是selenium v. 2.53),然后

pip install selenium==3.3.1

这次,当我运行脚本时,它提示缺少geckodriver: 因此,
wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux32.tar.gz

tar -xvzf geckodriver-v0.23.0-linux32.tar.gz

chmod 755 geckodriver (I also tried 777)

mv geckodriver /usr/local/bin/ (so it's in my PATH)

现在当我启动脚本时,这是我得到的错误信息:

Traceback (most recent call last):

  File "./basic0", line 13, in <module>
driver = webdriver.Firefox()

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
keep_alive=True)

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
self.error_handler.check_response(response)

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: connection refuse

火狐浏览器窗口弹出,当脚本停止时关闭。
3个回答

3

您将返回None的方法调用(clear())分配给了title变量,而您需要定义WebElement并随后调用方法,如下所示:

title = driver.find_element_by_id("subject")
title.clear()
title.send_keys("title")

我尝试了你的解决方案,首先出现了 "AttributeError: 'WebDriver' object has no attribute 'clear'" 的错误。然后我删除了 title.clear();它可以工作,但是输入框中填充的是 'title' 而不是变量内容。接着我尝试了 driver.send_keys(title):它可以正常工作。现在只是想知道为什么 clear() 没有起作用。非常感谢! - achille
2
@achille,我猜你尝试使用driver.clear()而不是title.clear()title应该是WebElement类型的对象)。 - Andersson

1
如果您能够点击文本框但无法输入,因为send_keys不起作用,并且您很匆忙或者只想单击而不选择任何元素,则需要安装此模块,请输入“pip install pyautogui”来安装。
import pyautogui


#use this line to type something 
pyautogui.typewrite('Anything you wanna type')

#use this line to press any key
pyautogui.press("esc")

[here][1] is the list of keys you can press
Note : If you are going to minimize the windows pyautogui will start typing at the place you currently clicking or when the pyautogui.typewite() is executing and you are on other pages it will start typing on the page you are in rather then typing on webpage.   

0
使用这个在Python中。它工作得很好。
    action = ActionChains(driver)
    EnterMobileNumber = self.driver.find_element(AppiumBy.ID, 'mention locator here')
    action.move_to_element(EnterMobileNumber).click(EnterMobileNumber).send_keys("8888888888").perform()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接