Here is a small test carried out with selenium in python:
locators = {
"entreprise_selection": ("ID", 'uidropdownCompanies'),
"entreprise_load": ("XPATH", "//a[@id='company_98']/div/span"),
"filter": ("XPATH", '//*[@id="filter"]')
}
def load(self, name):
self.entreprise_selection.click_button()
self.filter.set_text(name)
self.entreprise_load.click()
And to call test:
entreprisePage.load(input.name_entreprise)
There's no problem, everything works fine!
Now I have a problem when I try to have a dynamic xpath:
instead of having "//a[@id='company_98']/div/span"
I am trying to pass the id.
At the test level I add a new parameter (the integer type id that I have to convert to a string to be able to concatenate the xpath):
entreprisePage.load(input.name_entreprise, input.id_entreprise)
At the class level I no longer use a locator for the click:
locators = {
"entreprise_selection": ("ID", 'uidropdownCompanies'),
"filter": ("XPATH", '//*[@id="filter"]')
}
def load(self, name, id_entreprise):
self.entreprise_selection.click_button()
self.filter.set_text(name)
self.driver.find_element(By.XPATH("//a[@id='company_" + str(id_entreprise) + "']/div/span")).click()
When running the test I got the following error message:
FAILED tests/web_tests/test_login.py::TestLogin::test_login[input0] - TypeError: 'str' object is not callable
thanks for your help
I've tried to send directly parameters in string format , same problem :
entreprisePage.load(input.name_entreprise, "98")
locators = {
"entreprise_selection": ("ID", 'uidropdownCompanies'),
"filter": ("XPATH", '//*[@id="filter"]')
}
def load(self, name, id_entreprise):
self.entreprise_selection.click_button()
self.filter.set_text(name)
self.driver.find_element(By.XPATH("//a[@id='company_" + id_entreprise + "']/div/span")).click()
FAILED tests/web_tests/test_login.py::TestLogin::test_login[input0] - TypeError: 'str' object is not callable
You got an error, because syntax of find_element
is not correct.
By.XPATH
is a variable, not a function and you try to call it as a function.
This is correct syntax:
self.driver.find_element(By.XPATH, "//a[@id='company_" + id_entreprise + "']/div/span").click()