mardi 12 juillet 2016

Selenium: find_elements_by_xpath with multiple OR conditions

I'm trying to get Selenium for Python to create a list of web elements that match an XPath with multiple or alternatives.

This code works for finding the first such element, when using find_element_by_xpath:

vegetables = ["carrots", "beans", "peas"]

for veg in vegetables:
    try:
        found_vegs = driver.find_element_by_xpath((
                                                    "//*[self::p or self::span]" +
                                                    "[text()[contains(., '%s')]]" % veg
                                                  ))
        print found_vegs
    except:
        pass
    else:
        break

The above will print a matching WebElement.

But if I exchange the code to find_elements_by_xpath, it doesn't find anything at all.

Is the syntax wrong, or is it logically not possible to find multiple WebElements in this way?

Aucun commentaire:

Enregistrer un commentaire