python - 如何验证文本字段是否具有我输入的值?
问题描述
我对 Python 和 Selenium 非常陌生,我认为我需要使用 Assert 命令来验证文本字段是否包含我通过 Selenium 输入的内容。
我已经搜索了一个小时,但找不到答案。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains as AC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
driver = webdriver.Ie()
driver.get("https://bie.farmersinsurance.com/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[1]/td[1]/input"))).send_keys("tess893")
element = driver.find_element_by_xpath("/html/body/div/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[1]/td[1]/input")
assert element.text == "tess893"
这是我的结果:
Traceback (most recent call last):
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 256, in handle_args
run_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 52, in run_main
runner(addr, name, kind == 'module', *extra, **kwargs)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\runner.py", line 32, in run
set_trace=False)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "c:\_TMP\Test2.py", line 18, in <module>
assert element.text == "tess893"
AssertionError
我不知道为什么我得到一个AssertionError
. 根据我阅读的内容,它不应该抛出任何错误。
解决方案
.text
不是您要查找的属性。
它返回元素标签之间的可见文本
你想要value
输入元素的。为了得到那个,我想你会使用element.get_attribute('value')
下面的断言语句应该有效
assert element.get_attribute('value') == "tess893"
推荐阅读
- javascript - 尝试从下拉列表中选择一个值
- python - Anaconda Navigator (1.9.7) 卡在离线模式 - Mac
- react-native - 在全新项目中升级到 0.61.1 后反应原生版本不匹配
- curl - 如何使用 curl 命令为 Watson NLU 指定输入文件?
- c - 如何将文件中的每个字符存储到c中的数组中
- python - 我无法使用 OpenCV 和 Python 拆分所有手写字母
- java - 如何在 Spring + Thymeleaf 上添加弹出窗口
- c++ - c++ 生成线程安全的随机数
- c# - 仅使用第一个数组值填充数组的 C# Excel 范围分配
- reactjs - 如何为主题覆盖设置引导材料设计反应