In my application i have a table with users. In my test i want to add new user and then check is the user is added, but the table can have more than 1 page, and when i'm looking that element i can find it just on first page. Here is my code:
def test_new_user(driver, username='jared144'):
login(driver, username="Admin", password="Password")
# add new user
add_new_user(driver, username)
#check if the new user added
assert is_element_present(driver, By.LINK_TEXT, "%s" % username)
def add_new_user(driver, username):
driver.find_element_by_css_selector("[id=btnAdd]").click()
driver.find_element_by_css_selector("[id=systemUser_employeeName_empName]").send_keys("Adelia Foxy")
driver.find_element_by_css_selector("[id=systemUser_userName]").send_keys("%s" % username)
driver.find_element_by_css_selector("[id=systemUser_password]").send_keys("12345678")
driver.find_element_by_css_selector("[id=systemUser_confirmPassword]").send_keys("12345678")
driver.find_element_by_css_selector("[id=btnSave]").click()
def is_element_present(driver, how, what):
try:
driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
def login(driver, username, password):
driver.get("http://hrm.seleniumminutes.com/")
driver.find_element_by_css_selector("[name=txtUsername]").send_keys(username)
driver.find_element_by_css_selector("[name=txtPassword]").send_keys(password)
driver.find_element_by_css_selector("[name=Submit]").click()
driver.find_element_by_css_selector("[id=menu_admin_viewAdminModule]").click()
I have an idea how to do that, something like:
if NoSuchElementException
find_element_by().click() #click second page
But i don't know how to realize this
In your exception, tell the program to click on the next page
element (you didn't indicate how you found it). If your question is how to find the element that will take you to the next page when clicked, open up Chrome or FF and press the F12
key to bring up the debugger, and search for the element on the page, get its ID, NAME, XPATH (or other unique identifier) and then click on it as you show. So something like this might work.
def is_element_present(driver, how, what):
try:
driver.find_element(by=how, value=what)
return True
except NoSuchElementException as e:
try:
driver.find_element(by=`xpath`, value=`next page xpath`).click()
return True
except NoSuchElementException as nse:
return False