开发者

how to scroll with selenium

I have here a challenge that I spent som开发者_C百科e time addressing.

Selenium tells me that it cannot click a link that is not visible, so that means I need to scroll my canvas? I am using ubuntu 10, firefox 3, selenium 0.1, ruby 1.9.2, and selenium-webdriver 2.5.0

My code is

driver = Selenium::WebDriver.for :firefox;
driver.get login_url

wait = Selenium::WebDriver::Wait.new(:timeout => 2)

wait.until {
    driver.find_element(:name => 'j_password')
}

driver.find_element(:name => 'j_username').send_keys(username)

driver.focus(:name => 'j_username')`

and it says that focus() is not defined. How should I modify my code to put the input element on screen?

Um, this is a hypothetical example, I really need to scroll so that some other element is on the screen, but for simplicity I would like to be able to scroll s.t. any element is on the screen, even


location_once_scrolled_into_view to scroll using ruby.
As per your question your element is not visible, so selenium web driver is unable to click on it.

Simple solution to this is:

  1. Store the xpath of the element which is visible and nearby to your element.
  2. Scroll till that visible element, to make your element visible.
  3. Ensure your element is now visible, click on it

eg code:

  element = dirver.find_element(:xpath, "xpath of nearby visible element")
  element.location_once_scrolled_into_view
  my_element = driver.find_element(:xpath, "xpath of your element")
  my_element.click


I had an element at the bottom of the page with no other element close enough to select first. Finally got around it by tabbing from the last field in the form on the page.

element.send_keys(:tab)


When the documentation says "not visible" it's not refering the view port, but to the status of the element on the page. If something is hidden with CSS it cannot be clicked.


If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:

 JavascriptExecutor js = (JavascriptExecutor)driver;
                        js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
                        "document.body.scrollHeight,document.documentElement.clientHeight));");

I'm not aware of ruby but the above code can be used as a java Script which will scroll down the whole page.You can even define the area you want to scroll in the view window by simply hard- coding the window.scrollTo(200,350);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜