使用Selenium Python Webdriver检查字段中输入的文本字符串是否正确填充

使用Selenium Python Webdriver检查字段中输入的文本字符串是否正确填充,python,selenium,automation,webdriver,assert,Python,Selenium,Automation,Webdriver,Assert,想象一下我想要断言或验证任何文本字段的情况。我使用脚本将我的任何文本键入一个字段,并且我希望验证/断言它是否正确地填充在正确的位置 我试过函数“包含文本”,但如果它没有出现在代码e中该怎么办。G文本字符串与某些文本相似 实际上适用于代码中预定义的文本字符串: element = driver.find_element_by_xpath('//*[contains(text(),"Sample text")]').text assert element.strip() == "Sample Te

想象一下我想要断言或验证任何文本字段的情况。我使用脚本将我的任何文本键入一个字段,并且我希望验证/断言它是否正确地填充在正确的位置

我试过函数“包含文本”,但如果它没有出现在代码e中该怎么办。G文本字符串与某些文本相似

实际上适用于代码中预定义的文本字符串:

element = driver.find_element_by_xpath('//*[contains(text(),"Sample text")]').text

assert element.strip() == "Sample Text"

要输入值并断言incluse
webdriverwait
get\u属性(“值”)


用XPATH更新



请看一看。然后编辑问题并分享你尝试过的内容(代码)和一个可复制的例子。我写下我的名字,然后我需要检查它。G通过断言函数,我可以使用xpath吗?我可以使用python语法吗?是的,您也可以使用xpath。请检查“使用xpath更新”选项。让我知道状态?我已将其放入我的答案中。@Luketensie:您不应该使用睡眠,而应该使用我建议的
WebDriverWait
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get("https://boxing-registration.herokuapp.com/")
nameval="Luke Teensie"
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#name-input"))).send_keys(nameval)
returnval=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#name-input"))).get_attribute("value")
assert returnval == nameval
driver.get("https://boxing-registration.herokuapp.com/")
nameval="Luke Teensie"
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@id='name-input']"))).send_keys(nameval)
returnval=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@id='name-input']"))).get_attribute("value")
assert returnval == nameval