So I’m trying to create a Python script that goes to a website. Looks at all the results of coins on the page. Clicks on all the results and checks if there is a telegram link on the page, if so copy it and add it to the print result, if not just print the coin name. I’m getting a TypeError: ‘NoneType’ object is not callable error on line 23, in
element.click() . I’m not sure how to fix it.
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
# Set up a webdriver to simulate a browser
driver = webdriver.Chrome()
# Send a request to the webpage
response = requests.get('https://www.coingecko.com/en/new-cryptocurrencies')
# Parse the response
soup = BeautifulSoup(response.text, 'html.parser')
# Find all elements with the class "coin-name"
coin_elements = soup.find_all(class_='coin-name')
# Iterate through the coin elements
for element in coin_elements:
# Extract the text from the element (the coin name)
coin_name = element.text
# Simulate a click on the element
element.click()
# Wait for the page to load
time.sleep(2)
# Parse the content of the coin's page
coin_soup = BeautifulSoup(driver.page_source, 'html.parser')
# Find the element containing the Telegram group link
telegram_element = coin_soup.find('a', href=re.compile(r'^https://t.me/'))
# Check if the element was found
if telegram_element is not None:
# Extract the link from the element
telegram_link = telegram_element['href']
# Print the coin name and link
print(f'{coin_name}: {telegram_link}')
else:
# Print a message if the element was not found
print(f'{coin_name}: No Telegram group found')
# Close the webdriver
driver.close()
error:
line 23, in <module>
element.click()
TypeError: 'NoneType' object is not callable
3
Answers
You cannot mix up selenium and BeautifulSoup. This
coin_elements = soup.find_all(class_='coin-name')
won’t give you list ofWebElement
s so you cannot performclick()
on it.See this thread to check what type
find_all
in BS returns: What is the return type of the find_all method in Beautiful Soup?use
find_elements_by_xpath
method for selecting clickable elements. Selenium doesn’t accept other datatypes forclick
action.You can try something like,
Note: From your traceback it looks like you have got
NoneType
, which could be the indicator for inspecting the selectors once.You don’t need to use selenium at all.
Using python requests and bs4 you can achieved that.
First fetch all the urls for the coins and then iterate it and get response from the page and get the telegram link.
Code:
Console output: