0

I am trying to extract tables from a website for multiple dates. I am using the following code to do so.

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)

But the above code is giving a TypeError as follows.

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

I tried the same element selectors in Rselenium and I was able to extract the desired table. Since I'm new to Python I'm not sure what this error means! According to what I read here and here it seems to me that, this error occurs when we try to redefine a built-in function(Please correct me, if i misunderstood). But I believe, I did not redefine any built-in function here. So why am I getting this error? And how can I solve this?

2 Answers2

0

page_source is a property of the WebDriver instance. So you can't invoke page_source as a function. You have tried:

driver.page_source()

Hence you see the error:

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

Solution

As a solution you can use either of the following solutions:

  • Print the page_source:

    print(driver.page_source)
    
  • Save the page_source in a variable and print:

    html = driver.page_source
    print(html)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

page_source is a property, not a function. It returns str so no need to call text on it

source = driver.page_source
Guy
  • 46,488
  • 10
  • 44
  • 88