Search code examples
pythonseleniumautomation

Issue while passing locator as a Variable to find_elements method in selenium with python


I am preparing a test automation suite Using Selenium-Python and encountered an issue. I am trying to get a list of web elements using the find elements method.

**Method 1:** elements = driver.find_elements(By.XPATH,"<My XPATH>") 

-> This is working fine and returns the elements correctly

**Method 2: **
locator = By.XPATH, "<My XPATH>"
elements = driver.find_elements(locator)

This is preferred method based on the POM model and code reusability. But this returns an error as follows

ERROR:root:Message: invalid argument: 'using' must be a string (Session info: chrome=107.0.5304.107) Stacktrace: Backtrace:


Solution

  • You should do this way:

    locator = By.XPATH, "<My XPATH>"
    elements = driver.find_elements(*locator)
    

    so that you unpack your tuple to argument list.