如何等待文本出现在元素中?

huangapple go评论66阅读模式
英文:

How do I wait for text to appear in an element?

问题

是否有可能等到STRONG元素下面的文本出现在页面上后再继续执行?文本内容始终会根据触发器而变化。

我尝试了这段代码,但出现了错误。

这是出现在上述STRONG标签文本之前的内容。

所以我尝试了这个if条件,但出现了错误。

英文:

Is it possible to wait until visibility of element present based on text inside the STRONG element below? The text contained is always changing based on the trigger.

<div class="alert message alert-success alert-danger" style="margin-top: 5px; width: 330px; height: auto;">
  <button class="close" type="button" data-dismiss="alert">x<button>
    <span>
      <strong>
        This Video Title has already been choosen - please choose a unique title that best describes the content of the video.
      </strong>
    </span>
</div>

I tried with this code but got an error

if wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[contains(text(),'This Video Title has already been choosen - please choose a unique title that best describes the content of the video.']"))):
        print("Success")
    else:
        print("Failed")

This is what appears before text inside STRONG above

<div class="alert message alert-success alert-danger" style="margin-top: 5px; width: 330px; height: auto;">
  <button class="close" type="button" data-dismiss="alert">x<button>
    <span>
      <strong>
        Proccessing...
      </strong>
    </span>
</div>

so i try the if condition and i got this error:

Traceback (most recent call last):
  File "C:\Python311\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\Zephyr\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 
39, in <module>
    cli.main()
  File "c:\Users\Zephyr\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Zephyr\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\Zephyr\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\Zephyr\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\Zephyr\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "e:\Coding Space\test.py", line 164, in <module>
    input_data()
  File "e:\Coding Space\test.py", line 105, in input_data
    if wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[contains(text(), 'This Video Title has already been choosen - please choose a unique title that best describes the content of the video.')]"))):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:182:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:394:5
element.find/</<@chrome://remote/content/marionette/element.sys.mjs:134:16

答案1

得分: 0

已存在一个ExpectedCondition可用于此目的,即等待指定文本出现在元素中,使用EC.text_to_be_present_in_element(locator, expected_string)

您可以这样使用:

expected_string = "This Video Title has already been chosen - please choose a unique title that best describes the content of the video."
wait = WebDriverWait(driver, 10)
try:
    wait.until(EC.text_to_be_present_in_element((By.XPATH, "//strong")), expected_string)
    print("Success")
except TimeoutException:
    print("Failed")

有关更多信息,请参阅文档


或者

if wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[contains(text(), 'This Video Title')]"))):
    print("Success")
else:
    print("Failed")
英文:

There is an ExpectedCondition already available to do just this... wait for specified text to appear in an element, EC.text_to_be_present_in_element(locator, expected_string).

You can use it like

expected_string = "This Video Title has already been choosen - please choose a unique title that best describes the content of the video."
wait = WebDriverWait(driver, 10)
try:
    wait.until(EC.text_to_be_present_in_element((By.XPATH, "//strong")), expected_string)
    print("Success")
except TimeoutException:
    print("Failed")

See the docs for more info.


Alternate

if wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[contains(text(), 'This Video Title')]"))):
        print("Success")
    else:
        print("Failed")

huangapple
  • 本文由 发表于 2023年4月20日 09:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定