TypeError: 'NoneType' object is not callable error invoking find_element on soup object using Selenium Python?

TypeError: 'NoneType' object is not callable error invoking find_element on soup object using Selenium Python?

我想用python对一些带有selenium的帖子发表评论。
这是我的代码:

html=driver.page_source
soup=BeautifulSoup(html,"html.parser")
input=soup.find_element_by_css_selector("body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.sH9wk._JgwE > div > form > textarea)
input.clear()
input.send_keys("blahblah")
input.submit()

错误信息如下:

TypeError                                 Traceback (most recent call last)
<ipython-input-93-250a282801db> in <module>
----> 1 input=soup.find_element_by_css_selector("body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.sH9wk._JgwE > div > form > textarea")
      2 input.clear()

TypeError: 'NoneType' object is not callable

根据代码行:

soup=BeautifulSoup(html,"html.parser")

soup是一个类型的对象,代表数据结构中的文档。

其中 find_element_by_css_selector() is a WebDriver / WebElement 方法。

因此您将无法在 soup 上调用 find_element_by_css_selector(),这是一个 BeautifulSoup类型对象。因此您会看到错误:

TypeError: 'NoneType' object is not callable

理想情况下,您需要在 实例上调用 find_element_by_css_selector(),如下所示:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://example.com/")
input=driver.find_element_by_css_selector("body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.sH9wk._JgwE > div > form > textarea)
input.clear()
input.send_keys("blahblah")
input.submit()