Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a element/tag exists on page

I am trying to check if a element exists on the webpage with class name of "message message-information", Except when I try I just get a AttributeError: ‘NoneType’.

base_name = "GeForce RTX 3070 Ti"
url = "https://www.evga.com/products/product.aspx?pn=08G-P5-3797-KL"

headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}

site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')

stock_info = soup.find(class_="message message-information").get_text() # The ID of the stock element
   
# Check if the element is on the page, if not then say there is stock
if stock_info is None:
    # If the element exists
    tools.nogpustock(base_name)
else:
    # if the element does not exist
    tools.hasgpustock(base_name)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

just do not call the .get_text(), if the tag doesn’t exist the value returned by soup.find(class_="message message-information") will be None and calling None.get_text() will trigger the AttributeError: 'NoneType'

your code might be like

base_name = "GeForce RTX 3070 Ti"
url = "https://www.evga.com/products/product.aspx?pn=08G-P5-3797-KL"

headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}

site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')

stock_info_tag = soup.find(class_="message message-information") # The ID of the stock element
   
# Check if the element is on the page, if not then say there is stock
if stock_info is None:
    # If the element exists
    tools.nogpustock(base_name)
else:
    # if the element does not exist
    tools.hasgpustock(base_name)
    stock_info = stock_info_tag.get_text()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading