元素不可点击错误 Ruby/Watir [英] Element is not clickable error Ruby / Watir
问题描述
在我的测试中,我尝试访问 etsy.com,进行搜索,点击结果,然后将商品添加到我的购物车.在我尝试单击添加到购物车"按钮之前,我可以执行所有操作.下面的代码实际上在 IRB 中有效,所以我知道我的定位器是可靠的,但是当我运行测试时,我得到一个元素在点错误处无法点击
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.6.0/lib/selenium/webdriver/remote/response.rb:71:in 'assert_ok':未知错误:元素在 (930, 586) 点不可点击 (Selenium::WebDriver::Error::UnknownError)(会话信息:chrome=61.0.3163.100)
这是我的测试
需要'watir'# 测试用户是否可以搜索商品并将其添加到购物车b = Watir::Browser.new :chrome开始b.转到http://etsy.com"b.text_field(:id => 'search-query').set '培根是我的精神动物过山车'b.button(:value => 'Search').present?b.button(:value => '搜索').clickb.p(:text =>/Bacon Spirit Animal Coaster/).clickb.select_list(:id =>'inventory-variation-select-0').option(:text =>'Single ($8.00)').selectb.button(:text =>/加入购物车/).clickif b.text.include?("您购物车中的商品")输入测试通过!"别的puts 测试失败!"结尾确保b.关闭结尾
这是按钮的页面 HTML.
按钮>
根据浏览器宽度(以及可能的其他因素),可能会在添加到购物车按钮上方浮动对话框.例如,当我的测试失败时,按钮顶部有一个开始对话框.Chrome 尝试按位置点击.如果另一个元素位于该位置的元素之上,Chrome 将抛出异常.
最简单的方法是直接触发点击事件绕过Chrome的检查:
# Watir >6.8.0:b.button(:text =>/加入购物车/).点击!# 注意感叹号# 瓦蒂尔 <6.8.0:b.button(:text =>/添加到购物车/).fire_event(:onclick)
可能有条件地工作的其他解决方案:
- 在单击按钮之前最大化浏览器 -
browser.window.maximize
.这可以将浮动元素从按钮上移开. - 关闭浮动对话框.
In my test, I am attempting to hit etsy.com, do a search, click on a result, and add the item to my cart. I'm able to do everything up until the point where I attempt to click on the 'add to cart' button. The code below actually works in the IRB so I know my locator is solid, but when I run the test I get an element is unclickable at point error
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.6.0/lib/selenium/webdriver/remote/response.rb:71:in 'assert_ok': unknown error: Element is not clickable at point (930, 586) (Selenium::WebDriver::Error::UnknownError)
(Session info: chrome=61.0.3163.100)
Here's my test
require 'watir'
# test that a user can search for and add an item to shopping cart
b = Watir::Browser.new :chrome
begin
b.goto "http://etsy.com"
b.text_field(:id => 'search-query').set 'bacon is my spirit animal coaster'
b.button(:value => 'Search').present?
b.button(:value => 'Search').click
b.p(:text => /Bacon Spirit Animal Coaster/).click
b.select_list(:id => 'inventory-variation-select-0').option(:text => 'Single ($8.00)').select
b.button(:text => /Add to cart/).click
if b.text.include?("item in your cart")
puts "Test passed!"
else
puts "Test failed!"
end
ensure
b.close
end
And here is the page HTML for the button.
<button class="btn-transaction" type="submit">
<div class="btn-text">Add to cart</div>
<div class="ui-toolkit">
<div class="btn-spinner spinner spinner-small display-none"></div>
</div>
</button>
Depending on the browser width (and likely other factors), there may be dialogs floating over the add to cart button. For example, when the test failed for me, there was a get started dialog on top of the button. Chrome attempts to click by a location. If another element is on top of your element at that location, Chrome will throw the exception.
The easiest solution is to bypass Chrome's check by directly triggering the click event:
# Watir > 6.8.0:
b.button(:text => /Add to cart/).click! # note the exclamation mark
# Watir < 6.8.0:
b.button(:text => /Add to cart/).fire_event(:onclick)
Other solutions that may conditionally work:
- Maximize the browser before clicking the button -
browser.window.maximize
. This can move the floating element away from the button. - Close the floating dialog.
这篇关于元素不可点击错误 Ruby/Watir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!