如何验证文本字段中的值是否与我输入的值相同?

2024-04-27 00:31:05 发布

您现在位置: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。根据我所读的,它不应该抛出任何错误。你知道吗


Tags: inpylinetableextensionsvscodeuserstr
2条回答

.text不是您要查找的属性。 It returns the visible text between the elements tags

您需要输入元素的value。 我想你应该用element.get_attribute('value')

下面的assert语句应该有效

assert element.get_attribute('value') == "tess893"

您的代码语法正确,但assert就是这样工作的。你知道吗

如果条件为真,assert (condition)将不起任何作用,但是如果条件为假,它将给出一个错误。你知道吗

“assert”断言element.text == "tess893",如果它给出AssertError,那么元素.text不等于“tess893”。可能是xpath错误,或者它包含了不同的文本,但不确定。你知道吗

相关问题 更多 >