我正在使用单元测试框架进行硒自动化测试。我的问题是,当我使用clear()
函数清除字段以更改文本时,我清除的文本再次出现。我不明白为什么在其他字段上也能正常工作,仅在登录页面上出现问题。我也尝试了其他方法,在键盘删除代码中提到过,但是又重新出现了
def test_login(self):
self.driver.find_element_by_name("Username").send_keys("")
self.driver.find_element_by_name("Password").click()
error_message1=self.driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div[2]/div/div/div/div[1]/div/form/div/div[1]/div/div/div[2]/div")
time.sleep(0.1)
print(error_message1.text)
#self.assertEqual('Username is required.', error_message1.text)
self.driver.find_element_by_name("Password").send_keys("")
self.driver.find_element_by_name("Username").click()
error_message2=self.driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div[2]/div/div/div/div[1]/div/form/div/div[2]/div/div/div[2]")
time.sleep(0.1)
print(error_message2.text)
#self.assertEqual('Password is required.', error_message2.text)
time.sleep(0.2)
self.driver.find_element_by_name("Username").send_keys("sdsads")
time.sleep(0.1)
self.driver.find_element_by_css_selector("button.white--text").click()
self.driver.find_element_by_name("Username").clear()
time.sleep(1.5)
error_message = self.driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div[2]/div/div/div/div[2]/div/p")
#self.assertEqual('password is not allowed to be empty', error_message.text)
print(error_message.text)
#time.sleep(1.5)
#self.driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div[2]/div/div/div/div[1]/div/form/div/div[1]/div/div/div[1]/div/input").clear()
self.driver.find_element_by_name("Password").send_keys("test123")
self.driver.find_element_by_css_selector("button.white--text").click()
time.sleep(1.5)
error_message3 = self.driver.find_element_by_xpath("/html/body/div/div/main/div/div/div/div[2]/div/div/div/div[2]")
#self.assertEqual('Invalid user user name', error_message3.text)
print(error_message3.text)
# self.driver.find_element_by_name("Username").clear()
#for i in range(1,6):
# time.sleep(1)
# self.driver.find_element_by_name("Username").sendKeys(Keys.DELETE);
time.sleep(0.3)
self.driver.find_element_by_name("Password").clear()
time.sleep(0.3)
self.driver.find_element_by_name("Username").clear()
#self.driver.find_element_by_name("Password").send_keys("dssada")
self.driver.find_element_by_css_selector("button.white--text").click()
time.sleep(1.5)
error_message3 = self.driver.find_element_by_xpath(
"/html/body/div/div/main/div/div/div/div[2]/div/div/div/div[2]")
#self.assertEqual('Invalid user user name', error_message3.text)
#if error_message1.is_displayed():
# print(error_message1.text)
# print("test pass")
#self.assert
#self.driver.find_element_by_css_selector("button.white--text").click()
#time.sleep(1.5)
#wait = WebDriverWait(self.driver, 1.5)
#alert=WebDriverWait(self.driver, 1.5).until(EC.presence_of_all_elements_located('By.XPATH' ,"//*[@id='app']/div/main/div/div/div/div[2]/div/div/div/div[2]/div/p"))
#error_message=self.driver.find_element_by_xpath("//*[@id='app']/div/main/div/div/div/div[2]/div/div/div/div[2]/div/p")
#if error_message.is_displayed():
# print(error_message.text)
#else:
# print("testfail")
#self.driver.quit()