Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';str';使用驱动程序时无法调用对象。page_source().text_Python_Selenium_Typeerror - Fatal编程技术网

Python 类型错误:';str';使用驱动程序时无法调用对象。page_source().text

Python 类型错误:';str';使用驱动程序时无法调用对象。page_source().text,python,selenium,typeerror,Python,Selenium,Typeerror,我试图从一个网站中提取多个日期的表。我使用下面的代码来实现这一点 from selenium import webdriver from selenium.webdriver.chrome.options import Options CHROMEDRIVER_PATH = 'C:/Users/asus/Downloads/chromedriver/chromedriver.exe' options = Options() options.add_argument('--headless'

我试图从一个网站中提取多个日期的表。我使用下面的代码来实现这一点

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


CHROMEDRIVER_PATH = 'C:/Users/asus/Downloads/chromedriver/chromedriver.exe'

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

URL = 'URL'
driver.get(URL)

driver.find_element_by_id('calender1').click()

driver.find_element_by_css_selector(css_slctr).click() #css_selector for date
driver.find_element_by_id('show').click()
driver.page_source().text
# # print(html)

但是上面的代码给出了一个TypeError,如下所示

TypeError                                 Traceback (most recent call last)
<ipython-input-35-a6ffd4caf5cf> in <module>
      9 
     10 # driver.page_source().text
---> 11 driver.page_source().text
     12 # # print(html)
TypeError: 'str' object is not callable
TypeError回溯(最近一次调用)
在里面
9
10#驱动程序。页面#源().文本
--->11驱动程序页\源()文本
12##打印(html)
TypeError:“str”对象不可调用
我在Rselenium中尝试了相同的元素选择器,并且能够提取所需的表。 因为我是Python新手,所以我不确定这个错误是什么意思!根据我读到的内容,在我看来,当我们试图重新定义内置函数时,就会出现这个错误(如果我误解了,请纠正我)。但我相信,我没有在这里重新定义任何内置函数。那我为什么会犯这个错误呢?如何解决这个问题?

是实例的属性。因此,您不能将
page\u source
作为函数调用。您尝试过:

driver.page_source()
因此,您会看到错误:

---> 11 driver.page_source().text
TypeError: 'str' object is not callable

解决方案 作为解决方案,您可以使用以下任一解决方案:

  • 打印
    页面\u源文件

    print(driver.page_source)
    
  • 页面\u source
    保存在变量中并打印:

    html = driver.page_source
    print(html)
    

    • page\u source
      是一个属性,而不是一个函数。它返回
      str
      ,因此无需对其调用
      text

      source = driver.page_source
      

      page\u source
      是一个属性,而不是一个函数。