如何解决硒中的“ AttributeError:'NoneType'对象没有属性'send_keys'”错误

时间:2019-10-12 15:14:59

标签: python selenium-webdriver

我想在文本框中输入内容。因此,我打开了网页,单击了文本框的区域,然后单击了texbox。但是我收到此错误

tab = tab.send_keys("title")
AttributeError: 'NoneType' object has no attribute 'send_keys'

这是我尝试的代码。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
import time

response = webdriver.Chrome('C:/Users\jag\PycharmProjects\seleniumTest\drivers\chromedriver.exe')
response.implicitly_wait(20)
response.get("https://reqbin.com/")

tab = response.find_element_by_xpath("//*[@id='reqForm']/ul/li[3]/a").click()

tab = tab.send_keys("title")

2 个答案:

答案 0 :(得分:2)

.click()返回无。您可能想单独进行操作:

tab = response.find_element_by_xpath("//*[@id='reqForm']/ul/li[3]/a")
tab.click()
tab.send_keys("title")

答案 1 :(得分:0)

由于.click()方法未返回任何内容-None,您遇到了错误。
首先,您需要点击Content按钮。
在网站的 textarea 中输入文本有些棘手。要激活 textarea ,您应点击特定的div。查找以下示例如何输入文本:

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


driver = webdriver.Chrome('C:/Users\jag\PycharmProjects\seleniumTest\drivers\chromedriver.exe')
wait = WebDriverWait(driver, 5)


driver.get("https://reqbin.com/")

# Content button
driver.find_element_by_css_selector("[href='#pills-req-content']").click()
# wait to be clickable and click to the DIV to activate textarea
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#pills-req-content .CodeMirror"))).click()
# enter text
driver.find_element_by_css_selector("#pills-req-content .CodeMirror textarea").send_keys("title")

要输入多行文本,请使用\n,例如:此文本为多行\n这是下一行