草庐IT

python Selenium : does not wait until page is loaded after a click() command

coder 2023-05-25 原文

有人知道如何等待页面加载吗?我尝试了我在网上找到的所有可能的变体,但根本不起作用。

我需要在触发 click() 命令后等待,Web 服务器上有一些内部脚本会欺骗检查,例如(我排除了导入所需模块的代码并使用标准命名约定):

WebDriverWait(browser, 10).until(lambda d: d.find_element_by_id(the_id))

browser.implicitly_wait(10) 

elem2=wait.until(EC.presence_of_element_located((By.ID,the_id)))

上述所有检查都不起作用,即使页面仍在加载,它们也会返回 True。这会导致我正在阅读的文本不完整,因为在 click() 命令之后页面没有完全加载。似乎在 python 中,命令 clickAndWait 不可用(但同样,它可能无法解决问题,因为其他测试也失败了)。理想情况下,会有一个命令等待整个网页加载完毕(无论页面的特定元素如何)。

我设法通过在循环中手动插入 time.sleep(5) 来解决问题,但这是次优的,因为它可能会减慢整个过程。如果可能,最好只等待严格要求的时间。

谢谢

最佳答案

基本上,如果页面是由 JavaScript 生成的,那么就无法判断它何时“完成”加载。

但是。我通常会尝试检索页面元素,捕获异常并继续尝试直到达到超时。我还可以检查该元素是否可见,而不仅仅是存在。

必须有一些 DOM 元素的可见性可以用来测试页面是否已“完成”加载。我的测试用例中通常有 wait_for_element_visibilitywait_for_element_presence 函数。

def wait_for_visibility(self, selector, timeout_seconds=10):
    retries = timeout_seconds
    while retries:
        try:
            element = self.get_via_css(selector)
            if element.is_displayed():
                return element
        except (exceptions.NoSuchElementException,
                exceptions.StaleElementReferenceException):
            if retries <= 0:
                raise
            else:
                pass

        retries = retries - 1
        time.sleep(pause_interval)
    raise exceptions.ElementNotVisibleException(
        "Element %s not visible despite waiting for %s seconds" % (
            selector, timeout_seconds)
    )

get_via_css 是我自己的功能之一,但我希望它相当清楚它在做什么。

关于 python Selenium : does not wait until page is loaded after a click() command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819671/

有关python Selenium : does not wait until page is loaded after a click() command的更多相关文章

  1. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  2. ruby-on-rails - `require' : cannot load such file -- rails/commands - 2

    我是Rails的新手,我刚刚使用以下命令从头开始在MacOSSierra上安装了Rails:$gpg--keyserverhkp://keys.gnupg.net--recv-keys409B6B1796C275462A1703113804BB82D39DC0E3\curl-sSLhttps://get.rvm.io|bash-sstable--ruby$rvm-vrvm1.29.1(latest)byMichalPapis,PiotrKuczynski,WayneE.Seguin[https://rvm.io/]$ruby-vruby2.4.0p0(2016-12-24revisio

  3. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  4. ruby 选项解析器 : hiding help text for a command option - 2

    Ruby“OptionParser将根据此描述自动为您生成帮助屏幕”[http://ruby.about.com/od/advancedruby/a/optionparser.htm]有没有办法删除命令选项的帮助文本。我可以使用隐藏命令,但有一个命令选项(开关)并隐藏其帮助上下文。 最佳答案 我能够为此拼凑出一个不太优雅的解决方案。它将隐藏主帮助屏幕中的选项,听起来它可能符合您的需要:require'optparse'options={}OptionParser.newdo|opts|opts.banner="Usage:#{$0}

  5. ruby-on-rails - rbenv : bundle: command not found on production server - 2

    我正在尝试部署Rails应用程序,但遇到错误DEBUG[1a70ba92]Command:cd/home/deploy/myapp/releases/20140615090226&&(PATH=$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATHRBENV_ROOT=~/.rbenvRBENV_VERSION=2.1.2~/.rbenv/bin/rbenvexecbundleinstall--binstubs/home/deploy/myapp/shared/bin--path/home/deploy/myapp/shared/bundle--withou

  6. ruby-on-rails - 我得到 rvm : command not found after installation of rvm - 2

    我知道上面的问题是很常见的问题。我已经阅读了关于这个主题的多篇文章。但是我没有得到任何解决方案。我已经在本地安装了rvm。我们已经有了安装文件。所以进入文件夹并运行安装命令。$./install然后我检查了Users主文件夹中的./rvm文件夹$cd~/.rvm文件夹存在。至此安装成功。现在我在命令行输入rvm$rvm我正在低于异常$rvm-sh:rvm:commandnotfound在阅读了stackoverflow中有关此问题的多篇文章后,我了解到我必须在.bash_profile中添加以下行,因为我使用的是MacOSX10.7.3[[-s"$HOME/.rvm/scripts/r

  7. ruby-on-rails - 错误 : Command 'test' not recognized - 2

    我在Rails服务器中使用c9.io进行开发。我执行railstest并且它可以正常工作。第二天,我执行相同的命令,但出现错误:无法识别命令“测试”我没有做任何更改。我怎样才能恢复这个命令?注意:rake测试完美运行,但ruby​​页面http://edgeguides.rubyonrails.org/testing.html和ruby​​onrails教程,MichaelHartlhttps://www.railstutorial.org/book/static_pages谈论railstest命令,它对我有用2天。这是我的gem文件source'https://rubygems.o

  8. ruby - Git Bash 给出 sh.exe : ruby: command not found - 2

    我在WindowsVistaBusiness上使用GitBash。我正在尝试安装Redmine。基本上在第4步,我需要在安装bundler之前安装RubyGem作为依赖项安装的一部分(http://www.redmine.org/projects/redmine/wiki/RedmineInstall)。我从https://rubygems.org/pages/download下载了ruby​​gems-2.2.2.zip.然后我将文件解压缩到桌面上的一个文件夹中。在GitBash中,我输入了rubysetup.rb,我得到了sh.exe:ruby​​:commandnotfound。

  9. ruby-on-rails - rails : Command not Found after successful install - 2

    所以我安装了ruby​​、gems和rails-但是每当我输入rails时,我都会收到rails:Commandnotfound.错误。我转储了我的本地gem,我将在下面包含***LOCALGEMS***actionmailer(3.2.7)actionpack(3.2.7)activemodel(3.2.7)activerecord(3.2.7)activeresource(3.2.7)activesupport(3.2.7)arel(3.0.2)bigdecimal(1.1.0)builder(3.0.0)bundler(1.1.5)daemon_controller(1.0.0)

  10. ruby - 无法运行 Jenkins Build - bundle : "command not found" - 2

    我目前正在尝试为我的一些cucumber任务运行jenkins构建。我所有的gem都是使用Bundler安装的。Gem存储在vendor文件夹中。但是,当我尝试在执行shell构建步骤中运行bundleinstall--deployment时,出现以下错误:StartedbyuseranonymousBuildinginworkspace/Users/Shared/Jenkins/Home/jobs/cukes/workspace[workspace]$/bin/sh-xe/var/folders/zz/zyxvpxvq6csfxvn_n0000004000001/T/hudson44

随机推荐