首页 文章

有没有办法确定使用Selenium和Watir在Chrome浏览器中是否可以点击某个元素?

提问于
浏览
2

据我了解,由于使用Selenium Webdriver和Chromedriver实现点击事件的方式,使用Chrome浏览器执行Web测试自动化时,此问题才真正有用 . 作为前言,我使用并意识到通过使用Watir元素函数“present?”来找到一个元素,据我所知,它基本上是“可见的?”的组合 . 并“存在?” . 我还可以根据需要找到一个带有Webdriver元素函数的元素,以识别元素是否存在,如果不存在则为异常提供救援 . 我想确定的是以下内容:

有时,由于缺乏对页面的响应性,将会找到页面元素并通过所有验证测试以确定它是否存在,但由于上述缺乏页面响应性而无法主动实际交互 . 使用Chrome浏览器(使用Chromedriver)尝试与这些元素进行交互将导致错误:

irb(main):003:0> @ browser.button(:id,“button_login”) . 现在? => true irb(main):004:0> @ browser.button(:id,“button_login”) . 单击Selenium :: WebDriver :: Error :: UnknownError:未知错误:元素...在点处不可点击( 915,nt将收到点击:...(会话信息:chrome = 66.0.3359.181)(驱动程序信息:chromedriver = 2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT 6.3.9600 x86_64)来自C:/ Ruby23 / lib /ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/response.rb:69:in'desert_ok'来自C:/Ruby23/lib/ruby/gems/2.3 .0 / gems / selenium-webdriver-3.4.4 / lib / selenium / webdriver / remote / response.rb:32:in'initialize'from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium -webdriver-3.4.4 / lib / selenium / webdriver / remote / http / common.rb:83:来自C的'new':/ Ruby23 / lib / ruby / gems / 2.3 / autance / thesew-Linux3.4 .4 / lib / selenium / webdriver / remote / http / common.rb:83:来自C的'create_response':/ Ruby23 / lib / luby / gems / 3.3.0 / gems / selenium-webdriver-3.4.4 / lib /selenium/webdriver/remote/http/default.rb:107:in'请求'来自C:/ Ruby23 / lib / ruby / gems / 2.3.0 / gems / selenium-webdriver-3.4.4 / lib / selenium / webdriver / remote / http / common.rb:61:来自C:/ Ruby23 / lib /的'call' ruby / gems / 2.3.0 / gems / selenium-webdriver-3.4.4 / lib / selenium / webdriver / remote / bridge.rb:170:来自C:/Ruby23/lib/ruby/gems/2.3的'execute' . 0 / gems / selenium-webdriver-3.4.4 / lib / selenium / webdriver / remote / oss / bridge.rb:579:在'执行'中来自C:/Ruby23/lib/ruby/gems/2.3.0/gems/ selenium-webdriver-3.4.4 / lib / selenium / webdriver / remote / oss / bridge.rb:328:来自C的'click_element':/ Ruby23 / lib / ruby / gems / 2.3.0 / gems / selenium-webdriver- 3.4.4 / lib / selenium / webdriver / common / element.rb:74:在C中的'click'中:/Ruby23 / lib / ruby / gems / 3.3.0 / gems / watir-6.4.1 / lib / watir / elements / element.rb:131:在C中的'block in click'中:/Ruby23 / lib / ruby / gems / 2.3.0 / gems / watir-6.4.1 / lib / watir / elements / element.rb:656:在C中的'element_call'中:/Ruby23 / lib / ruby / gems / 2.3.0 / gems / watir-6.4.1 / lib / watir / elements / element.rb:122:来自(irb)的'click':4来自C:/Ruby23/bin/irb.cmd:19:'''

我知道我现在可以解救,但这需要我实际点击该元素 . 基本上我想写一个特殊功能“可点击?”这将返回一个布尔输出而不实际单击该元素并可能导航离开该页面 . 我宁愿不尝试使用--ctrl单击类型,如果新窗口返回true,关闭窗口,将焦点设置在第一个窗口,救援返回false - 工作流程 .

2 回答

  • 1

    不幸的是,目前(Watir 6.11)不是一种检查元素是否可点击的方法 - 除了实际尝试点击它之外 . 将来可能会出现 - 请参阅https://github.com/watir/watir/issues/532中的讨论,我们尝试自动重试点击 .

    我会尝试等待重叠的元素消失 .

    如果重叠类似于最终会消失的叠加层,那么它相对简单 - 例如:

    browser.element(id: 'overlapping_element').wait_while(&:present?)
    

    如果重叠元素移动而不是消失或者您不知道重叠元素,则可以尝试近似重叠元素检查 . 当Chrome点击某个元素时,它会获取该元素的中心位置,然后点击该点 . 如果该点的顶级元素不是您的元素,则抛出异常 . 以下等待将执行此检查,直到没有重叠元素:

    target = browser.button
    target_children = target.elements.to_a
    browser.wait_until do
      location = target.location
      size = target.size
      center = [location.x + size.width/2, location.y + size.height/2]
      element_at_point = browser.execute_script("return document.elementFromPoint(#{center[0]}, #{center[1]});")
      [target, target_children].flatten.include?(element_at_point)
    end
    target.click
    

    请注意,我之前没有这样做,所以我不知道是否有边缘情况 . 似乎可以使用Chrome和Firefox .

  • 0

    我建议你等待这个按钮显示在网页上 . 我遇到了同样的问题(我在测试中使用了XPath) . 要解决这个问题:

    首先,我定义了2个辅助方法,因为我必须重复使用它们 . 一个用于搜索页面上的确切元素(此方法通常需要一段时间才能返回结果,因此您不需要 sleep 浏览器)和一个用于单击给定"id"的按钮 .

    module Helpers
      module Common
        def wait_for_element_id(value)
          find(:xpath, "(//*[@id='#{value}'])[1]")
        end
    
        def click_button_with_id(value)
          first(:xpath, "//button[@id='#{value}']").click
        end
      end
    end
    

    在测试之后,您可以使用辅助方法,如:

    it 'clicks on the login button and magic is executed' do
      logout(user)
      wait_for_element_id('button_login')
      click_button_with_id('button_login')
    
      expect(magic).to be_executed
    end
    

    我也不确定,但是由于浏览器窗口大小(由于尺寸太小而未显示按钮)或者因为测试的“无头”模式,您也可以体验相同的项目 .

相关问题