Home>

We want to perform page processing based on the existence of class.

If class "bonus" exists and I want to confirm by printing "Yes":

# Class "bonus" actually exists but not printed
if driver.find_element_by_class_name ("bonus") == True:
    print ("Yes")

# Error below
if len (driver.find_element_by_class_name ("bonus"))>0:
    print ("Yes")
TypeError: object of type 'WebElement' has no len ()

# Using try will print correctly
try:
    driver.find_element_by_class_name ("bonus")
    print ("Yes")
except:
    print ("No")
Yes

I would like to process with an if statement without using a try. Is this possible?
I'm sure it's a naive question, but I'd be happy if you could tell me.
Thanks for your cooperation.

  • Answer # 1

    How about using the following method?
    Since the return value is a list, is it possible to determine the presence or absence of the target tag by obtaining its length?

    find_elements_by_class_name