tortuga
tortuga

Reputation: 61

Change if statements to something more functional in Python + Selenium

I have similar list of if statements like this that repeat the same pattern. How can I crunch this code to make it look better and not be so monstrous?

header = driver.find_element(By.CLASS_NAME, "header")
    if (header):
        print header.get_attribute("class") + present
    else:
        print header.get_attribute("class") + not_present

    t = driver.find_element(By.CLASS_NAME, "t")
    if (t):
        print t.get_attribute("class") + present
    else:
        print t.get_attribute("class") + not_present

    origin = driver.find_element(By.CLASS_NAME, "origin")
    if (origin):
        print origin.get_attribute("class") + present
    else:
        print origin.get_attribute("class") + not_present

    desk= driver.find_element(By.CLASS_NAME, "desk")
    if (desk):
        print desk.get_attribute("class") + present
    else:
        print desk.get_attribute("class") + not_present

    act = driver.find_element(By.CLASS_NAME, "act")
    if (act):
        print act.get_attribute("class") + present
    else:
        print act.get_attribute("class") + not_present

Upvotes: 0

Views: 182

Answers (3)

alecxe
alecxe

Reputation: 473873

The call to driver.find_element() would either result in a WebElement instance, or it would throw an error. There is no sense to check whether it is truthy or not.

Instead catch an error:

from selenium.common.exceptions import WebDriverException

try:
    header = driver.find_element(By.CLASS_NAME, "header")
    print header.get_attribute("class")
except WebDriverException:  # still better to handle a more specific error here
    print "Not found"

Note the find_element() method is intended to be used privately:

‘Private’ method used by the find_element_by_* methods.

Use find_element_by_class_name() instead:

driver.find_element_by_class_name("header")

Aside from that, @Brien's approach to loop over the class names is something you should apply.

Upvotes: 2

Kyle Shrader
Kyle Shrader

Reputation: 952

You may run into issues if the element is not present, as you cannot get the attribute "class". To fix this, I'd suggest using the string you are searching for (ie. "header").

You can use the ternary conditional operator to remove code duplication in your conditionals.

Example:

header = driver.find_element(By.CLASS_NAME, "header")
print "header" + present if header else not_present

Since you are acting on all of the elements in the same manner, you could generalize your code as well.

Example:

def print_is_present(by, identifier):
    element = driver.find_element(by, identifier)
    print identifier, present if element else not_present

classes = ['header','t','origin','desk','act']
for class_name in classes:
    print_is_present(By.CLASS_NAME, class_name)

Upvotes: 0

Blair
Blair

Reputation: 6693

You should be able to handle this with a fairly simple loop over a list since everything is so similar. Something like this:

names = ['header','t','origin','desk','act']
for name in names:
    element = driver.find_element(By.CLASS_NAME,name)
    print element.get_attribute('class') + (present if element else not_present)

Upvotes: 3

Related Questions