【问题标题】:selenium 'nonetype' object has no attribute 'send_keys'selenium 'nonetype' 对象没有属性 'send_keys'
【发布时间】:2019-12-25 15:24:45
【问题描述】:
title='this is the title'

我想用 python/selenium 在网页中定位,这一行:

<input id="subject" name="subject" maxlength="50" value="" class="nude error" type="text">

我将此代码与 python-selenium 一起使用(在 Debian 下):

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'

注意:当脚本因为这个错误而停止时,鼠标光标在网页内的右侧;但我找不到发送键来填写输入的方法

我也试过了:

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']")

但它不起作用;而且鼠标光标不在正确的位置。

然后我尝试了更高版本的硒:

我完全清除了 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

firefox窗口弹出,脚本停止后关闭

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您已经分配了方法调用 (clear()),它返回 Nonetitle 变量,而您需要定义 WebElement 并随后调用方法为

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

    【讨论】:

    • 我尝试了您的解决方案并首先得到:“AttributeError: 'WebDriver' object has no attribute 'clear'”。然后我删除了 title.clear();它工作但用“标题”而不是变量内容填充输入。然后我尝试了 driver.send_keys(title): 没关系。现在只是想知道为什么 clear() 不起作用。真的谢谢你!
    • @achille ,我猜你试过driver.clear() 而不是title.clear()title 应该是WebElement 类型的对象)
    【解决方案2】:

    如果您可以在文本框中单击,但由于 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.   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多