有没有一种方法可以确定使用Selenium和Watir在Chrome浏览器中是否可以单击某个元素? [英] Is there a way to determine if an element is clickable in Chrome Browser using Selenium and Watir?

查看:61
本文介绍了有没有一种方法可以确定使用Selenium和Watir在Chrome浏览器中是否可以单击某个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,由于使用Selenium Webdriver和Chromedriver实现click事件的方式,这个问题实际上仅在使用Chrome浏览器执行网络测试自动化时才有意义.作为序言,我使用并且意识到通过使用Watir元素函数"present?"来查找元素,据我所知,该函数本质上是"visible?"的组合.和存在?".我还可以根据需要找到具有Webdriver元素功能的元素,以识别是否存在针对异常的紧急情况.我要确定的是以下内容:

As I understand it, this question is really only relevant when performing web test automation using Chrome browser due to the manner in which a click event is implemented with Selenium Webdriver and Chromedriver. To preface, I use and am aware of finding an element through the use of the Watir element function "present?", which as I understand is essentially a combination of "visible?" and "exists?". I could also, at need find an element with Webdriver element functions to identify if an element is present with a rescue for an exception if it is not. What I am trying to determine is the following:

有时,由于缺乏对页面的响应能力,将找到页面元素,并通过所有页面存在的验证测试,但由于上述缺乏页面响应能力,因此无法积极地与之交互.使用Chrome浏览器(通过Chromedriver)尝试与这些元素进行交互将导致错误:

At times, due to a lack of responsiveness to a page, there will be page elements that will be found and pass all validation tests for it's existence, but cannot actively actually be interacted with due to the aforementioned lack of page responsiveness. Using Chrome browser (with Chromedriver) attempts to interact with these elements will result in the error:

irb(main):003:0> @ browser.button(:id,"button_login").present? =>是的 irb(main):004:0> @ browser.button(:id,"button_login").click Selenium :: WebDriver :: Error :: UnknownError:未知错误:元素...在点(915, nt将获得点击:

...
(会议信息:chrome = 66.0.3359.181) (驱动程序信息:chromedriver = 2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),平台= 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:在'assert_ok'中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/response.rb:32:in'initialize' 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/common.rb:83:在'new'中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/common.rb:83:在'create_response'中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/default.rb:107:在'request'中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/common.rb:61:在'call'中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/bridge.rb:170:在'execute'中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/oss/bridge.rb:579:在'execute'中 来自C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/oss/bridge.rb:328:在'click_element'中 来自C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/common/element.rb:74:在'click'中 来自C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-6.4.1/lib/watir/elements/element.rb:131:在阻止点击"中 从C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-6.4.1/lib/watir/elements/element.rb:656:在'element_call'中 来自C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-6.4.1/lib/watir/elements/element.rb:122:在'click'中 来自(irb):4 来自C:/Ruby23/bin/irb.cmd:19:in''

irb(main):003:0> @browser.button(:id, "button_login").present? => true irb(main):004:0> @browser.button(:id, "button_login").click Selenium::WebDriver::Error::UnknownError: unknown error: Element ... is not clickable at point (915, nt would receive the click:

...
(Session info: chrome=66.0.3359.181) (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.3.9600 x86_64) from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/response.rb:69:in 'assert_ok' from 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:in 'new' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/common.rb:83:in 'create_response' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/default.rb:107:in 'request' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/http/common.rb:61:in 'call' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/bridge.rb:170:in 'execute' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/oss/bridge.rb:579:in 'execute' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/remote/oss/bridge.rb:328:in 'click_element' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.4.4/lib/selenium/webdriver/common/element.rb:74:in 'click' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-6.4.1/lib/watir/elements/element.rb:131:in 'block in click' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-6.4.1/lib/watir/elements/element.rb:656:in 'element_call' from C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-6.4.1/lib/watir/elements/element.rb:122:in 'click' from (irb):4 from C:/Ruby23/bin/irb.cmd:19:in ''

我知道我现在可以进行救援,但这意味着我实际上单击了元素.本质上,我想编写一个特殊的函数"clickable?"它将返回布尔输出,而无需实际单击该元素并可能离开页面.我不希望使用--ctrl + click的类型尝试此操作,如果新窗口返回true,则关闭窗口,将焦点放在第一个窗口上,抢救返回false--工作流程.

I know I can rescue at this point, but that entails that I actually click the element. Essentially I want to write a special function "clickable?" that will return a boolean output without actually clicking the element and possibly navigating away from the page. I would prefer not to attempt this with a type of --ctrl+click, if new window return true, close window, set focus on first window, rescue return false-- workflow.

推荐答案

Watir 6.15.0 +

Element#obscured?来检查这种情况.您现在可以执行以下操作:

Element#obscured? has been added to check for this scenario. You can now do:

browser.element(id: 'target').wait_while(&:obscured?).click

Watir 6.15.0之前的版本

对于较旧的版本,您将需要采用其他方法.

For older versions, you will need to take a different approach.

我会尝试等待重叠的元素消失.如果重叠是最终会消失的叠加层,则相对简单-例如:

I would try waiting for the overlapping element to go away. If the overlapping is something like an overlay that will eventually disappear, it's relatively straightforward - eg:

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

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

If the overlapping element gets moved rather than disappears or you don't know the overlapping element, you could try approximating the overlapping element check. When Chrome clicks an element, it gets the element's center location and then clicks at that point. If there the top-level element at that point is not your element, the exception is thrown. The following wait will do this check until there is no overlapping element:

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一起使用.

Note that I haven't had to do this before, so I don't know if there are edge cases. Seemed to work with Chrome and Firefox.

这篇关于有没有一种方法可以确定使用Selenium和Watir在Chrome浏览器中是否可以单击某个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆