1. <legend id='WNvUI'><style id='WNvUI'><dir id='WNvUI'><q id='WNvUI'></q></dir></style></legend>
    2. <i id='WNvUI'><tr id='WNvUI'><dt id='WNvUI'><q id='WNvUI'><span id='WNvUI'><b id='WNvUI'><form id='WNvUI'><ins id='WNvUI'></ins><ul id='WNvUI'></ul><sub id='WNvUI'></sub></form><legend id='WNvUI'></legend><bdo id='WNvUI'><pre id='WNvUI'><center id='WNvUI'></center></pre></bdo></b><th id='WNvUI'></th></span></q></dt></tr></i><div id='WNvUI'><tfoot id='WNvUI'></tfoot><dl id='WNvUI'><fieldset id='WNvUI'></fieldset></dl></div>
        <bdo id='WNvUI'></bdo><ul id='WNvUI'></ul>

      1. <tfoot id='WNvUI'></tfoot>

        <small id='WNvUI'></small><noframes id='WNvUI'>

      2. 等到元素中的文本被更改

        时间:2023-09-10
        <tfoot id='GdvNx'></tfoot>

      3. <legend id='GdvNx'><style id='GdvNx'><dir id='GdvNx'><q id='GdvNx'></q></dir></style></legend>

            <i id='GdvNx'><tr id='GdvNx'><dt id='GdvNx'><q id='GdvNx'><span id='GdvNx'><b id='GdvNx'><form id='GdvNx'><ins id='GdvNx'></ins><ul id='GdvNx'></ul><sub id='GdvNx'></sub></form><legend id='GdvNx'></legend><bdo id='GdvNx'><pre id='GdvNx'><center id='GdvNx'></center></pre></bdo></b><th id='GdvNx'></th></span></q></dt></tr></i><div id='GdvNx'><tfoot id='GdvNx'></tfoot><dl id='GdvNx'><fieldset id='GdvNx'></fieldset></dl></div>

              <small id='GdvNx'></small><noframes id='GdvNx'>

                <tbody id='GdvNx'></tbody>
              • <bdo id='GdvNx'></bdo><ul id='GdvNx'></ul>
                  本文介绍了等到元素中的文本被更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  请建议 Selenium 是否有一个不错的选择来等到元素内的文本发生更改.条件:

                  Please suggest if Selenium has a good option to wait until text will be changed within an element. Conditions:

                  • 页面未自动重新加载
                  • 动态重新加载我需要的文本的元素
                  • 更新此数据所需的时间未知
                  • 预期的文字未知.这是一个时间戳.

                  我编写了一个方法,它每 1 秒(或我设置的任何时间)检查一次,并在更改时获取值.但我认为可能有一些更好的选择.我尽量避免使用 time.sleep,但在目前的情况下,这是我唯一的选择.注意,此文本可能会在 5 分钟后更改,因此我需要调整重试次数 retriestime.sleep()另外,我使用 print 只是为了调试.

                  I wrote a method which checks it every 1 second (or any time I set) and gets the value when it is changed. But I think there may be some better options. I try to avoid using time.sleep, but in the current case this is the only option I came to. Note, this text may change even after 5 minutes, so I will need to adjust the number of retries retries or time.sleep() Also, I use print just for debugging.

                  def wait_for_text(driver):  
                      init_text = "init text"
                      retry = 1
                      retries = 120
                      start_time = time.time()
                      while retry <= retries:
                          time.sleep(1)
                          field_text = driver.find_element_by_id("my_id").text
                          if field_text != init_text:
                              break
                          retry += 1
                          now = time.time()
                          total_time = now-start_time
                          print(f"Total time for text to change {total_time}")
                      print(f"Final field value: {field_text}")
                  

                  推荐答案

                  你可以这样做:

                  driver.get("https://stackoverflow.com")
                  init_text = "Log in"
                  from selenium.webdriver.support.wait import WebDriverWait
                  waiter = WebDriverWait(driver=driver, timeout=120, poll_frequency=1)
                  waiter.until(lambda drv: drv.find_element_by_class_name("login-link").text != init_text)
                  print("Success")
                  driver.quit()
                  

                  所以你基本上是在等待一个元素停止等于给定值.

                  So you are basically waiting for an element stops being equal to a given value.

                  P.S. - 您可以通过更改开发工具中的按钮文本来测试上面的代码片段.

                  P.S. - You can test the snippet above by changing the button text in dev tools.

                  这篇关于等到元素中的文本被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python - 安装了扩展的远程 Webdriver 下一篇:Selenium webdriver 在单击之前突出显示元素

                  相关文章

                  最新文章

                  <i id='8jo3n'><tr id='8jo3n'><dt id='8jo3n'><q id='8jo3n'><span id='8jo3n'><b id='8jo3n'><form id='8jo3n'><ins id='8jo3n'></ins><ul id='8jo3n'></ul><sub id='8jo3n'></sub></form><legend id='8jo3n'></legend><bdo id='8jo3n'><pre id='8jo3n'><center id='8jo3n'></center></pre></bdo></b><th id='8jo3n'></th></span></q></dt></tr></i><div id='8jo3n'><tfoot id='8jo3n'></tfoot><dl id='8jo3n'><fieldset id='8jo3n'></fieldset></dl></div>

                  <legend id='8jo3n'><style id='8jo3n'><dir id='8jo3n'><q id='8jo3n'></q></dir></style></legend>
                • <small id='8jo3n'></small><noframes id='8jo3n'>

                    <tfoot id='8jo3n'></tfoot>
                      <bdo id='8jo3n'></bdo><ul id='8jo3n'></ul>