Selenium Python 在几页上查找元素
Selenium Python find element on few pages
在我的应用程序中,我有一个 table 用户。在我的测试中,我想添加新用户,然后检查是否添加了用户,但 table 可以有多个页面,当我查看该元素时,我只能在第一页上找到它。
这是我的代码:
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()
我知道怎么做了,比如:
if NoSuchElementException
find_element_by().click() #click second page
但是我不知道怎么实现
在你的例外情况下,告诉程序点击 next page
元素(你没有说明你是如何找到它的)。如果您的问题是如何找到单击时将带您进入下一页的元素,请打开 Chrome 或 FF 并按 F12
键调出调试器,然后在页面,获取其 ID、NAME、XPATH(或其他唯一标识符),然后在显示时单击它。所以这样的事情可能会奏效。
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
在我的应用程序中,我有一个 table 用户。在我的测试中,我想添加新用户,然后检查是否添加了用户,但 table 可以有多个页面,当我查看该元素时,我只能在第一页上找到它。 这是我的代码:
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()
我知道怎么做了,比如:
if NoSuchElementException
find_element_by().click() #click second page
但是我不知道怎么实现
在你的例外情况下,告诉程序点击 next page
元素(你没有说明你是如何找到它的)。如果您的问题是如何找到单击时将带您进入下一页的元素,请打开 Chrome 或 FF 并按 F12
键调出调试器,然后在页面,获取其 ID、NAME、XPATH(或其他唯一标识符),然后在显示时单击它。所以这样的事情可能会奏效。
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