TypeError:“非类型”对象不可调用使用Selenium Python调用soup对象上的find_元素时出错?

2024-10-06 12:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: divinputbyhtmlarticlesectionbodyelement
1条回答
网友
1楼 · 发布于 2024-10-06 12:37:45

根据代码行:

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

soup类型的对象,表示数据结构中的文档

其中as^{}WebDriver/WebElement方法

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

TypeError: 'NoneType' object is not callable

理想情况下,您需要在WebDriver实例上调用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()

相关问题 更多 >