Home > database >  Selenium Multiple Values for find_elements?
Selenium Multiple Values for find_elements?

Time:03-28

In python I have:

driver.find_elements(by=By.TAG_NAME, value='a')

How can I change this line to include also b, c, d etc... values?

I tried this but it didn't work:

driver.find_elements(by=By.TAG_NAME, value='a', Value='b')...

CodePudding user response:

If we take a look at the source code of find_elements

def find_elements(self, by=By.ID, value=None):
    """
    Find elements given a By strategy and locator. Prefer the find_elements_by_* methods when
    possible.

    :Usage:
        elements = driver.find_elements(By.CLASS_NAME, 'foo')

    :rtype: list of WebElement

it takes only two args, so having this driver.find_elements(by=By.TAG_NAME, value='a', Value='b') will not work.

However, to address the issue that you've reported here, I do not think TAG_NAME can be used as a locator strategies

Thus using CSS you could do this:

driver.find_elements(By.CSS_SELECTOR, "tag_name[attributeA = 'valueA'][attributeB = 'valueB']")

Using XPath:

driver.find_elements(By.XPATH, "//tag_name[@attributeA = 'valueA' and @attributeB = 'valueB']")

CodePudding user response:

To answer to your question lets say you want to check multiple HTML tag using find elements. The best way you could do is CSS_SELECTOR or XPATH.

By Using CSS Selector let say you want check div or span or input tag

Your code should be like

driver.find_elements(By.CSS_SELECTOR,"div, span, input")

By XPATH

driver.find_elements(By.XPATH,"//*[self::div|self::span|self::input]")

Hope this example will help.

  • Related