0

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
1
  • Where are originator and destination defined? Commented Jun 23, 2015 at 18:21

3 Answers 3

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Just one minor caveat to the above excellent answer. If you are using Python < 2.5 (IIRC) the ternary expression present if element else not_present will not work, and you'd have to revert to the if element print.. else print...
2

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.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.