'typeerror 'nonetype' object is not callable selenium

Code:

from selenium import webdriver

from bs4 import BeautifulSoup

driver=webdriver.Chrome('H:\datascience-python\selinium\chromedriver.exe')

driver.get('https://www.aljazeera.com/news/')

button = driver.find_element_by_id('btn_showmore_b1_418')

driver.execute_script("arguments[0].click();", button)

content = driver.page_source

soup = BeautifulSoup(content, 'html.parser')

container = soup.select('div.topics-sec-item-cont')

titlelist = []

urllist = []

for items in  container:

    if items is not None:

        title = items.find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a/h2').text

        url = items.find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a')

        titlelist.append(title)

        urllist.append(url.get_attribute('href'))

        print(str(titlelist) + '\n')

        print(str(urllist) + '\n')

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

<ipython-input-1-acf307cfccb3> in <module>

     18 for items in  container:

     19     if items is not None:

---> 20         title = items.find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a/h2').text

     21         url = items.find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a')

     22 

TypeError: 'NoneType' object is not callable    


Solution 1:[1]

title = items.find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a/h2').text

the xpath provided in this line of code is not returning object. This issue can arise only because of two reason:

  1. Either the provided xpath is wrong.
  2. The div that you are trying to extract from is not yet loaded completely.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Khamkar