如何查找有时出现在页面上的文本?一个典型的例子是错误的密码。然后会出现一条消息,指出密码输入不正确,我需要对此消息进行断言。该消息出现在该 block <td colspan = "2" style = "color: Red;" align = "center"> Wrong name or password.</td>.
中不幸的是,我使用 xpath 输入的代码找不到我的文本。
参见:
wait = WebDriverWait (driver 10)
text_tag = wait.until (EC.presence_of_element_located ((By.XPATH, "/html/body/form/div[3]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/div/div/div/div/div/div/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/b")))
if text_tag:
assert text_tag.text == "Incorrect name or password."
print (text_tag.text)
print ("Found - OK")
else:
exit()
最佳答案
好的,谢谢帮助。这是最终的函数脚本。有必要将 XPATH 更改为 CSS 选择器:
error_message = driver.find_element_by_css_selector('td[style = "color: Red;"]')
if error_message:
assert error_message.text == "Incorrect name or password."
print (error_message.text)
print ("Found - OK")
else:
exit()
print("Not FOUND")
driver.implicitly_wait(0)
关于python - 如何识别 Selenium 中偶尔出现的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56662303/