Selenium::WebDriver::Error::MoveTargetOutOfBoundsError: Element cannot be scrolled into view:[object HTMLSpanElement]

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

Something strange is happening on this page:

require "watir-webdriver" b = Watir::Browser.new b.goto "http://mideastunes.com/" b.div(class: "feat-container").element(css: "a span").present?  => true  b.div(class: "feat-container").element(css: "a span").click  Selenium::WebDriver::Error::MoveTargetOutOfBoundsError: Element cannot be scrolled into view:[object HTMLSpanElement]     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/default.rb:64:in `request'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:590:in `raw_execute'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:568:in `execute'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:350:in `clickElement'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/common/element.rb:34:in `click'     from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/watir-webdriver-0.5.3/lib/watir-webdriver/elements/element.rb:107:in `click'     from (irb):48     from /Users/zeljko/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

I am trying to click this span:

<span class="feature-prev-icon icon"></span>

I can click it with this:

b.span(class: "feature-prev-icon icon").click

but I would like to click it with this:

b.div(class: "feat-container").element(css: "a span").click

Environment: Mac OS X 10.7.3, Firefox 10.0.1, ruby 1.9.2p290, selenium-webdriver 2.19.0, watir-webdriver 0.5.3

回答1:

Edit: This isn't a bug.

As other answerers have indicated, the element you're interacting with is off-screen and can't be scrolled in to screen other than by complex interaction.

This probably does what you want:

b.div(class: "feat-container").elements(css: "a span")[2].click

What you probably want to do in this case is rather than checking:

b.div(class: "feat-container").element(css: "a span").present?

Looping over each child, checking that its bounding box is on screen, specifically in this case that the left coordinate is non-negative.

This is a bit unintuitive of WebDriver - unfortunately, there isn't really a good way to handle the underlying problem.



回答2:

I think your problem stems from what you're trying to click.

puts b.div(class: "feat-container").element(css: "a span").attribute_value("class")

returns

play-icon play

Which is the play button on a slide that has been 'hidden' off to the left using positioning and left:-ahugevalue

Watir-webdriver is not being buggy here, it's just telling you it can't click it as it can't scroll that far left as there's no horizontal scrollbar.

edit: by the way the system used to test this differs greatly to the one commented above. I don't live in fear of breaking everything at home like I do at work. Everything is up to date. Firefox 10.0.1, Ruby 1.9.2, Watir-webdriver 0.5.3, Selenium-webdriver 2.19.0 on Windows 7.



回答3:

I had this issue too. There is also a ticket.

The problem could be caused by a weird unscrollable layout, so the button could only be reached if the window was bigger.

So resizing the window, could fix it.

in java it was:

driver.manage().window().setPosition(new Point(0, 0)); driver.manage().window().setSize(new Dimension(1100, 800));


回答4:

You can also use to bring the element into view (and maximize the page):

page.driver.browser.manage.window.maximize


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!