Watir. Scroll to a certain point of the page

前端 未结 3 1511
别跟我提以往
别跟我提以往 2020-12-30 12:48

I am trying to automate an online survey on a website but I get this error each time:

Selenium::WebDriver::Error::UnknownError: unknown error: Element is not         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 13:06

    Using execute_script

    To scroll to an element, you will need to execute javascript:

    browser.execute_script('arguments[0].scrollIntoView();', button)
    

    This can be seen to be working in the following script. Without the line to scroll, a chat tab overlays one of the buttons causing an exception.

    require 'watir-webdriver'
    
    browser = Watir::Browser.new :chrome
    browser.goto 'https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd'
    
    buttons = browser.elements(:class => "assessment-choice")
    
    buttons.each do |button|
        browser.execute_script('arguments[0].scrollIntoView();', button)
        button.click
    end
    

    Using the watir-scroll gem

    Note that you can install the watir-scroll gem to make the scrolling line nicer. The gem allows the line to simply be:

    browser.scroll.to button
    

    The script would then look like:

    require 'watir-webdriver'
    require 'watir-scroll'
    
    browser = Watir::Browser.new :chrome
    browser.goto 'https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd'
    
    buttons = browser.elements(:class => "assessment-choice")
    
    buttons.each do |button|
        browser.scroll.to button
        button.click
    end
    

提交回复
热议问题