HOME> 2010世界杯> Scroll wheel actions

Scroll wheel actions

Scroll wheel actionsA representation of a scroll wheel input device for interacting with a web page.

Selenium v4.2

Chromium Only

There are 5 scenarios for scrolling on a page.

Scroll to elementThis is the most common scenario. Unlike traditional click and send keys methods,

the actions class does not automatically scroll the target element into view,

so this method will need to be used if elements are not already inside the viewport.

This method takes a web element as the sole argument.

Regardless of whether the element is above or below the current viewscreen,

the viewport will be scrolled so the bottom of the element is at the bottom of the screen.

Java

Python

CSharp

Ruby

JavaScript

Kotlin WebElement iframe = driver.findElement(By.tagName("iframe"));

new Actions(driver)

.scrollToElement(iframe)

.perform();View Complete Code

View on GitHubexamples/java/src/test/java/dev/selenium/actions_api/WheelTest.java

Copy

Closepackage dev.selenium.actions_api;

import dev.selenium.BaseChromeTest;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.interactions.WheelInput;

public class WheelTest extends BaseChromeTest {

@Test

public void shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

new Actions(driver)

.scrollToElement(iframe)

.perform();

Assertions.assertTrue(inViewport(iframe));

}

@Test

public void shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

int deltaY = footer.getRect().y;

new Actions(driver)

.scrollByAmount(0, deltaY)

.perform();

Assertions.assertTrue(inViewport(footer));

}

@Test

public void shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);

new Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html");

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

private boolean inViewport(WebElement element) {

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

return (boolean) ((JavascriptExecutor) driver).executeScript(script, element);

}

}

iframe = driver.find_element(By.TAG_NAME, "iframe")

ActionChains(driver)\

.scroll_to_element(iframe)\

.perform()View Complete Code

View on GitHubexamples/python/tests/actions_api/test_wheel.py

Copy

Closefrom time import sleep

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.common.actions.wheel_input import ScrollOrigin

def test_can_scroll_to_element(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

ActionChains(driver)\

.scroll_to_element(iframe)\

.perform()

assert _in_viewport(driver, iframe)

def test_can_scroll_from_viewport_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

delta_y = footer.rect['y']

ActionChains(driver)\

.scroll_by_amount(0, delta_y)\

.perform()

sleep(0.5)

assert _in_viewport(driver, footer)

def test_can_scroll_from_element_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

scroll_origin = ScrollOrigin.from_element(iframe)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_element_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

scroll_origin = ScrollOrigin.from_element(footer, 0, -50)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_viewport_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

scroll_origin = ScrollOrigin.from_viewport(10, 10)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def _in_viewport(driver, element):

script = (

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

"return f\n"

"window.pageYOffset&&t+o>window.pageXOffset"

)

return driver.execute_script(script, element)

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

new Actions(driver)

.ScrollToElement(iframe)

.Perform();View Complete Code

View on GitHubexamples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs

Copy

Closeusing System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;

using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI

{

[TestClass]

public class WheelTest : BaseChromeTest

{

[TestMethod]

public void ShouldAllowScrollingToAnElement()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

new Actions(driver)

.ScrollToElement(iframe)

.Perform();

Assert.IsTrue(IsInViewport(iframe));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

int deltaY = footer.Location.Y;

new Actions(driver)

.ScrollByAmount(0, deltaY)

.Perform();

Assert.IsTrue(IsInViewport(footer));

}

[TestMethod]

public void ShouldScrollFromElementByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = iframe

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromElementByGivenAmountWithOffset()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = footer,

XOffset = 0,

YOffset = -50

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmountFromOrigin()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html";

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Viewport = true,

XOffset = 10,

YOffset = 10

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

private bool IsInViewport(IWebElement element)

{

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

IJavaScriptExecutor javascriptDriver = this.driver as IJavaScriptExecutor;

return (bool)javascriptDriver.ExecuteScript(script, element);

}

}

} iframe = driver.find_element(tag_name: 'iframe')

driver.action

.scroll_to(iframe)

.performView Complete Code

View on GitHubexamples/ruby/spec/actions_api/wheel_spec.rb

Copy

Close# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Scrolling' do

let(:driver) { start_session }

it 'scrolls to element' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

driver.action

.scroll_to(iframe)

.perform

expect(in_viewport?(iframe)).to eq true

end

it 'scrolls by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

delta_y = footer.rect.y

driver.action

.scroll_by(0, delta_y)

.perform

expect(in_viewport?(footer)).to eq true

end

it 'scrolls from element by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(iframe)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls from element by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(footer, 0, -50)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(10, 10)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

end

def in_viewport?(element)

in_viewport = <<~IN_VIEWPORT

for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;

e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;

return f

window.pageYOffset&&t+o>window.pageXOffset

IN_VIEWPORT

driver.execute_script(in_viewport, element)

end

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 0, iframe)

.perform()

View Complete Code

View on GitHubexamples/javascript/test/actionsApi/wheelTest.spec.js

Copy

Closeconst { By, Browser, Builder} = require('selenium-webdriver')

const assert = require('assert')

describe('Actions API - Wheel Tests', function () {

let driver

before(async function () {

driver = await new Builder().forBrowser('chrome').build();

})

after(async() => await driver.quit())

it('Scroll to element', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 0, iframe)

.perform()

assert.ok(await inViewport(iframe))

})

it('Scroll by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const footer = await driver.findElement(By.css("footer"))

const deltaY = (await footer.getRect()).y

await driver.actions()

.scroll(0, 0, 0, deltaY)

.perform()

await driver.sleep(500)

assert.ok(await inViewport(footer))

})

it('Scroll from an element by a given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 200, iframe)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an element with an offset', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

const footer = await driver.findElement(By.css("footer"))

await driver.actions()

.scroll(0, -50, 0, 200, footer)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an offset of origin (element) by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(10, 10, 0, 200)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

function inViewport(element) {

return driver.executeScript("for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset", element)

}

}) val iframe = driver.findElement(By.tagName("iframe"))

Actions(driver)

.scrollToElement(iframe)

.perform()View Complete Code

View on GitHubexamples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt

Copy

Closepackage dev.selenium.actions_api

import dev.selenium.BaseTest

import org.junit.jupiter.api.Assertions

import org.junit.jupiter.api.Test

import org.openqa.selenium.By

import org.openqa.selenium.JavascriptExecutor

import org.openqa.selenium.WebElement

import org.openqa.selenium.interactions.Actions

import org.openqa.selenium.interactions.WheelInput

class WheelTest : BaseTest() {

@Test

fun shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

Actions(driver)

.scrollToElement(iframe)

.perform()

Assertions.assertTrue(inViewport(iframe))

}

@Test

fun shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val deltaY = footer.getRect().y

Actions(driver)

.scrollByAmount(0, deltaY)

.perform()

Assertions.assertTrue(inViewport(footer))

}

@Test

fun shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50)

Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

fun inViewport(element: WebElement): Boolean {

val script = "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset"

return (driver as JavascriptExecutor).executeScript(script, element) as Boolean

}

}

Scroll by given amountThis is the second most common scenario for scrolling. Pass in an delta x and a delta y value for how much to scroll

in the right and down directions. Negative values represent left and up, respectively.

Java

Python

CSharp

Ruby

JavaScript

Kotlin WebElement footer = driver.findElement(By.tagName("footer"));

int deltaY = footer.getRect().y;

new Actions(driver)

.scrollByAmount(0, deltaY)

.perform();View Complete Code

View on GitHubexamples/java/src/test/java/dev/selenium/actions_api/WheelTest.java

Copy

Closepackage dev.selenium.actions_api;

import dev.selenium.BaseChromeTest;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.interactions.WheelInput;

public class WheelTest extends BaseChromeTest {

@Test

public void shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

new Actions(driver)

.scrollToElement(iframe)

.perform();

Assertions.assertTrue(inViewport(iframe));

}

@Test

public void shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

int deltaY = footer.getRect().y;

new Actions(driver)

.scrollByAmount(0, deltaY)

.perform();

Assertions.assertTrue(inViewport(footer));

}

@Test

public void shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);

new Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html");

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

private boolean inViewport(WebElement element) {

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

return (boolean) ((JavascriptExecutor) driver).executeScript(script, element);

}

}

footer = driver.find_element(By.TAG_NAME, "footer")

delta_y = footer.rect['y']

ActionChains(driver)\

.scroll_by_amount(0, delta_y)\

.perform()View Complete Code

View on GitHubexamples/python/tests/actions_api/test_wheel.py

Copy

Closefrom time import sleep

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.common.actions.wheel_input import ScrollOrigin

def test_can_scroll_to_element(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

ActionChains(driver)\

.scroll_to_element(iframe)\

.perform()

assert _in_viewport(driver, iframe)

def test_can_scroll_from_viewport_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

delta_y = footer.rect['y']

ActionChains(driver)\

.scroll_by_amount(0, delta_y)\

.perform()

sleep(0.5)

assert _in_viewport(driver, footer)

def test_can_scroll_from_element_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

scroll_origin = ScrollOrigin.from_element(iframe)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_element_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

scroll_origin = ScrollOrigin.from_element(footer, 0, -50)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_viewport_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

scroll_origin = ScrollOrigin.from_viewport(10, 10)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def _in_viewport(driver, element):

script = (

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

"return f\n"

"window.pageYOffset&&t+o>window.pageXOffset"

)

return driver.execute_script(script, element)

IWebElement footer = driver.FindElement(By.TagName("footer"));

int deltaY = footer.Location.Y;

new Actions(driver)

.ScrollByAmount(0, deltaY)

.Perform();View Complete Code

View on GitHubexamples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs

Copy

Closeusing System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;

using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI

{

[TestClass]

public class WheelTest : BaseChromeTest

{

[TestMethod]

public void ShouldAllowScrollingToAnElement()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

new Actions(driver)

.ScrollToElement(iframe)

.Perform();

Assert.IsTrue(IsInViewport(iframe));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

int deltaY = footer.Location.Y;

new Actions(driver)

.ScrollByAmount(0, deltaY)

.Perform();

Assert.IsTrue(IsInViewport(footer));

}

[TestMethod]

public void ShouldScrollFromElementByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = iframe

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromElementByGivenAmountWithOffset()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = footer,

XOffset = 0,

YOffset = -50

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmountFromOrigin()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html";

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Viewport = true,

XOffset = 10,

YOffset = 10

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

private bool IsInViewport(IWebElement element)

{

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

IJavaScriptExecutor javascriptDriver = this.driver as IJavaScriptExecutor;

return (bool)javascriptDriver.ExecuteScript(script, element);

}

}

} footer = driver.find_element(tag_name: 'footer')

delta_y = footer.rect.y

driver.action

.scroll_by(0, delta_y)

.performView Complete Code

View on GitHubexamples/ruby/spec/actions_api/wheel_spec.rb

Copy

Close# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Scrolling' do

let(:driver) { start_session }

it 'scrolls to element' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

driver.action

.scroll_to(iframe)

.perform

expect(in_viewport?(iframe)).to eq true

end

it 'scrolls by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

delta_y = footer.rect.y

driver.action

.scroll_by(0, delta_y)

.perform

expect(in_viewport?(footer)).to eq true

end

it 'scrolls from element by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(iframe)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls from element by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(footer, 0, -50)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(10, 10)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

end

def in_viewport?(element)

in_viewport = <<~IN_VIEWPORT

for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;

e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;

return f

window.pageYOffset&&t+o>window.pageXOffset

IN_VIEWPORT

driver.execute_script(in_viewport, element)

end

const footer = await driver.findElement(By.css("footer"))

const deltaY = (await footer.getRect()).y

await driver.actions()

.scroll(0, 0, 0, deltaY)

.perform()

View Complete Code

View on GitHubexamples/javascript/test/actionsApi/wheelTest.spec.js

Copy

Closeconst { By, Browser, Builder} = require('selenium-webdriver')

const assert = require('assert')

describe('Actions API - Wheel Tests', function () {

let driver

before(async function () {

driver = await new Builder().forBrowser('chrome').build();

})

after(async() => await driver.quit())

it('Scroll to element', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 0, iframe)

.perform()

assert.ok(await inViewport(iframe))

})

it('Scroll by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const footer = await driver.findElement(By.css("footer"))

const deltaY = (await footer.getRect()).y

await driver.actions()

.scroll(0, 0, 0, deltaY)

.perform()

await driver.sleep(500)

assert.ok(await inViewport(footer))

})

it('Scroll from an element by a given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 200, iframe)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an element with an offset', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

const footer = await driver.findElement(By.css("footer"))

await driver.actions()

.scroll(0, -50, 0, 200, footer)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an offset of origin (element) by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(10, 10, 0, 200)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

function inViewport(element) {

return driver.executeScript("for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset", element)

}

}) val footer = driver.findElement(By.tagName("footer"))

val deltaY = footer.getRect().y

Actions(driver)

.scrollByAmount(0, deltaY)

.perform()View Complete Code

View on GitHubexamples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt

Copy

Closepackage dev.selenium.actions_api

import dev.selenium.BaseTest

import org.junit.jupiter.api.Assertions

import org.junit.jupiter.api.Test

import org.openqa.selenium.By

import org.openqa.selenium.JavascriptExecutor

import org.openqa.selenium.WebElement

import org.openqa.selenium.interactions.Actions

import org.openqa.selenium.interactions.WheelInput

class WheelTest : BaseTest() {

@Test

fun shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

Actions(driver)

.scrollToElement(iframe)

.perform()

Assertions.assertTrue(inViewport(iframe))

}

@Test

fun shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val deltaY = footer.getRect().y

Actions(driver)

.scrollByAmount(0, deltaY)

.perform()

Assertions.assertTrue(inViewport(footer))

}

@Test

fun shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50)

Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

fun inViewport(element: WebElement): Boolean {

val script = "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset"

return (driver as JavascriptExecutor).executeScript(script, element) as Boolean

}

}

Scroll from an element by a given amountThis scenario is effectively a combination of the above two methods.

To execute this use the “Scroll From” method, which takes 3 arguments.

The first represents the origination point, which we designate as the element,

and the second two are the delta x and delta y values.

If the element is out of the viewport,

it will be scrolled to the bottom of the screen, then the page will be scrolled by the provided

delta x and delta y values.

Java

Python

CSharp

Ruby

JavaScript

Kotlin WebElement iframe = driver.findElement(By.tagName("iframe"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();View Complete Code

View on GitHubexamples/java/src/test/java/dev/selenium/actions_api/WheelTest.java

Copy

Closepackage dev.selenium.actions_api;

import dev.selenium.BaseChromeTest;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.interactions.WheelInput;

public class WheelTest extends BaseChromeTest {

@Test

public void shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

new Actions(driver)

.scrollToElement(iframe)

.perform();

Assertions.assertTrue(inViewport(iframe));

}

@Test

public void shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

int deltaY = footer.getRect().y;

new Actions(driver)

.scrollByAmount(0, deltaY)

.perform();

Assertions.assertTrue(inViewport(footer));

}

@Test

public void shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);

new Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html");

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

private boolean inViewport(WebElement element) {

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

return (boolean) ((JavascriptExecutor) driver).executeScript(script, element);

}

}

iframe = driver.find_element(By.TAG_NAME, "iframe")

scroll_origin = ScrollOrigin.from_element(iframe)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()View Complete Code

View on GitHubexamples/python/tests/actions_api/test_wheel.py

Copy

Closefrom time import sleep

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.common.actions.wheel_input import ScrollOrigin

def test_can_scroll_to_element(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

ActionChains(driver)\

.scroll_to_element(iframe)\

.perform()

assert _in_viewport(driver, iframe)

def test_can_scroll_from_viewport_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

delta_y = footer.rect['y']

ActionChains(driver)\

.scroll_by_amount(0, delta_y)\

.perform()

sleep(0.5)

assert _in_viewport(driver, footer)

def test_can_scroll_from_element_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

scroll_origin = ScrollOrigin.from_element(iframe)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_element_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

scroll_origin = ScrollOrigin.from_element(footer, 0, -50)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_viewport_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

scroll_origin = ScrollOrigin.from_viewport(10, 10)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def _in_viewport(driver, element):

script = (

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

"return f\n"

"window.pageYOffset&&t+o>window.pageXOffset"

)

return driver.execute_script(script, element)

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = iframe

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();View Complete Code

View on GitHubexamples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs

Copy

Closeusing System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;

using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI

{

[TestClass]

public class WheelTest : BaseChromeTest

{

[TestMethod]

public void ShouldAllowScrollingToAnElement()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

new Actions(driver)

.ScrollToElement(iframe)

.Perform();

Assert.IsTrue(IsInViewport(iframe));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

int deltaY = footer.Location.Y;

new Actions(driver)

.ScrollByAmount(0, deltaY)

.Perform();

Assert.IsTrue(IsInViewport(footer));

}

[TestMethod]

public void ShouldScrollFromElementByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = iframe

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromElementByGivenAmountWithOffset()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = footer,

XOffset = 0,

YOffset = -50

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmountFromOrigin()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html";

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Viewport = true,

XOffset = 10,

YOffset = 10

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

private bool IsInViewport(IWebElement element)

{

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

IJavaScriptExecutor javascriptDriver = this.driver as IJavaScriptExecutor;

return (bool)javascriptDriver.ExecuteScript(script, element);

}

}

} iframe = driver.find_element(tag_name: 'iframe')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(iframe)

driver.action

.scroll_from(scroll_origin, 0, 200)

.performView Complete Code

View on GitHubexamples/ruby/spec/actions_api/wheel_spec.rb

Copy

Close# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Scrolling' do

let(:driver) { start_session }

it 'scrolls to element' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

driver.action

.scroll_to(iframe)

.perform

expect(in_viewport?(iframe)).to eq true

end

it 'scrolls by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

delta_y = footer.rect.y

driver.action

.scroll_by(0, delta_y)

.perform

expect(in_viewport?(footer)).to eq true

end

it 'scrolls from element by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(iframe)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls from element by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(footer, 0, -50)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(10, 10)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

end

def in_viewport?(element)

in_viewport = <<~IN_VIEWPORT

for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;

e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;

return f

window.pageYOffset&&t+o>window.pageXOffset

IN_VIEWPORT

driver.execute_script(in_viewport, element)

end

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 200, iframe)

.perform()

View Complete Code

View on GitHubexamples/javascript/test/actionsApi/wheelTest.spec.js

Copy

Closeconst { By, Browser, Builder} = require('selenium-webdriver')

const assert = require('assert')

describe('Actions API - Wheel Tests', function () {

let driver

before(async function () {

driver = await new Builder().forBrowser('chrome').build();

})

after(async() => await driver.quit())

it('Scroll to element', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 0, iframe)

.perform()

assert.ok(await inViewport(iframe))

})

it('Scroll by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const footer = await driver.findElement(By.css("footer"))

const deltaY = (await footer.getRect()).y

await driver.actions()

.scroll(0, 0, 0, deltaY)

.perform()

await driver.sleep(500)

assert.ok(await inViewport(footer))

})

it('Scroll from an element by a given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 200, iframe)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an element with an offset', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

const footer = await driver.findElement(By.css("footer"))

await driver.actions()

.scroll(0, -50, 0, 200, footer)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an offset of origin (element) by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(10, 10, 0, 200)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

function inViewport(element) {

return driver.executeScript("for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset", element)

}

}) val iframe = driver.findElement(By.tagName("iframe"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()View Complete Code

View on GitHubexamples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt

Copy

Closepackage dev.selenium.actions_api

import dev.selenium.BaseTest

import org.junit.jupiter.api.Assertions

import org.junit.jupiter.api.Test

import org.openqa.selenium.By

import org.openqa.selenium.JavascriptExecutor

import org.openqa.selenium.WebElement

import org.openqa.selenium.interactions.Actions

import org.openqa.selenium.interactions.WheelInput

class WheelTest : BaseTest() {

@Test

fun shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

Actions(driver)

.scrollToElement(iframe)

.perform()

Assertions.assertTrue(inViewport(iframe))

}

@Test

fun shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val deltaY = footer.getRect().y

Actions(driver)

.scrollByAmount(0, deltaY)

.perform()

Assertions.assertTrue(inViewport(footer))

}

@Test

fun shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50)

Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

fun inViewport(element: WebElement): Boolean {

val script = "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset"

return (driver as JavascriptExecutor).executeScript(script, element) as Boolean

}

}

Scroll from an element with an offsetThis scenario is used when you need to scroll only a portion of the screen, and it is outside the viewport.

Or is inside the viewport and the portion of the screen that must be scrolled

is a known offset away from a specific element.

This uses the “Scroll From” method again, and in addition to specifying the element,

an offset is specified to indicate the origin point of the scroll. The offset is

calculated from the center of the provided element.

If the element is out of the viewport,

it first will be scrolled to the bottom of the screen, then the origin of the scroll will be determined

by adding the offset to the coordinates of the center of the element, and finally

the page will be scrolled by the provided delta x and delta y values.

Note that if the offset from the center of the element falls outside of the viewport,

it will result in an exception.

Java

Python

CSharp

Ruby

JavaScript

Kotlin WebElement footer = driver.findElement(By.tagName("footer"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);

new Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform();View Complete Code

View on GitHubexamples/java/src/test/java/dev/selenium/actions_api/WheelTest.java

Copy

Closepackage dev.selenium.actions_api;

import dev.selenium.BaseChromeTest;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.interactions.WheelInput;

public class WheelTest extends BaseChromeTest {

@Test

public void shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

new Actions(driver)

.scrollToElement(iframe)

.perform();

Assertions.assertTrue(inViewport(iframe));

}

@Test

public void shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

int deltaY = footer.getRect().y;

new Actions(driver)

.scrollByAmount(0, deltaY)

.perform();

Assertions.assertTrue(inViewport(footer));

}

@Test

public void shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);

new Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html");

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

private boolean inViewport(WebElement element) {

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

return (boolean) ((JavascriptExecutor) driver).executeScript(script, element);

}

}

footer = driver.find_element(By.TAG_NAME, "footer")

scroll_origin = ScrollOrigin.from_element(footer, 0, -50)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()View Complete Code

View on GitHubexamples/python/tests/actions_api/test_wheel.py

Copy

Closefrom time import sleep

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.common.actions.wheel_input import ScrollOrigin

def test_can_scroll_to_element(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

ActionChains(driver)\

.scroll_to_element(iframe)\

.perform()

assert _in_viewport(driver, iframe)

def test_can_scroll_from_viewport_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

delta_y = footer.rect['y']

ActionChains(driver)\

.scroll_by_amount(0, delta_y)\

.perform()

sleep(0.5)

assert _in_viewport(driver, footer)

def test_can_scroll_from_element_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

scroll_origin = ScrollOrigin.from_element(iframe)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_element_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

scroll_origin = ScrollOrigin.from_element(footer, 0, -50)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_viewport_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

scroll_origin = ScrollOrigin.from_viewport(10, 10)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def _in_viewport(driver, element):

script = (

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

"return f\n"

"window.pageYOffset&&t+o>window.pageXOffset"

)

return driver.execute_script(script, element)

IWebElement footer = driver.FindElement(By.TagName("footer"));

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = footer,

XOffset = 0,

YOffset = -50

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();View Complete Code

View on GitHubexamples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs

Copy

Closeusing System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;

using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI

{

[TestClass]

public class WheelTest : BaseChromeTest

{

[TestMethod]

public void ShouldAllowScrollingToAnElement()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

new Actions(driver)

.ScrollToElement(iframe)

.Perform();

Assert.IsTrue(IsInViewport(iframe));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

int deltaY = footer.Location.Y;

new Actions(driver)

.ScrollByAmount(0, deltaY)

.Perform();

Assert.IsTrue(IsInViewport(footer));

}

[TestMethod]

public void ShouldScrollFromElementByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = iframe

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromElementByGivenAmountWithOffset()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = footer,

XOffset = 0,

YOffset = -50

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmountFromOrigin()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html";

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Viewport = true,

XOffset = 10,

YOffset = 10

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

private bool IsInViewport(IWebElement element)

{

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

IJavaScriptExecutor javascriptDriver = this.driver as IJavaScriptExecutor;

return (bool)javascriptDriver.ExecuteScript(script, element);

}

}

} expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html')View Complete Code

View on GitHubexamples/ruby/spec/actions_api/wheel_spec.rb

Copy

Close# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Scrolling' do

let(:driver) { start_session }

it 'scrolls to element' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

driver.action

.scroll_to(iframe)

.perform

expect(in_viewport?(iframe)).to eq true

end

it 'scrolls by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

delta_y = footer.rect.y

driver.action

.scroll_by(0, delta_y)

.perform

expect(in_viewport?(footer)).to eq true

end

it 'scrolls from element by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(iframe)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls from element by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(footer, 0, -50)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(10, 10)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

end

def in_viewport?(element)

in_viewport = <<~IN_VIEWPORT

for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;

e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;

return f

window.pageYOffset&&t+o>window.pageXOffset

IN_VIEWPORT

driver.execute_script(in_viewport, element)

end

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

View Complete Code

View on GitHubexamples/javascript/test/actionsApi/wheelTest.spec.js

Copy

Closeconst { By, Browser, Builder} = require('selenium-webdriver')

const assert = require('assert')

describe('Actions API - Wheel Tests', function () {

let driver

before(async function () {

driver = await new Builder().forBrowser('chrome').build();

})

after(async() => await driver.quit())

it('Scroll to element', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 0, iframe)

.perform()

assert.ok(await inViewport(iframe))

})

it('Scroll by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const footer = await driver.findElement(By.css("footer"))

const deltaY = (await footer.getRect()).y

await driver.actions()

.scroll(0, 0, 0, deltaY)

.perform()

await driver.sleep(500)

assert.ok(await inViewport(footer))

})

it('Scroll from an element by a given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 200, iframe)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an element with an offset', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

const footer = await driver.findElement(By.css("footer"))

await driver.actions()

.scroll(0, -50, 0, 200, footer)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an offset of origin (element) by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(10, 10, 0, 200)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

function inViewport(element) {

return driver.executeScript("for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset", element)

}

}) val footer = driver.findElement(By.tagName("footer"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50)

Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform()View Complete Code

View on GitHubexamples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt

Copy

Closepackage dev.selenium.actions_api

import dev.selenium.BaseTest

import org.junit.jupiter.api.Assertions

import org.junit.jupiter.api.Test

import org.openqa.selenium.By

import org.openqa.selenium.JavascriptExecutor

import org.openqa.selenium.WebElement

import org.openqa.selenium.interactions.Actions

import org.openqa.selenium.interactions.WheelInput

class WheelTest : BaseTest() {

@Test

fun shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

Actions(driver)

.scrollToElement(iframe)

.perform()

Assertions.assertTrue(inViewport(iframe))

}

@Test

fun shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val deltaY = footer.getRect().y

Actions(driver)

.scrollByAmount(0, deltaY)

.perform()

Assertions.assertTrue(inViewport(footer))

}

@Test

fun shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50)

Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

fun inViewport(element: WebElement): Boolean {

val script = "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset"

return (driver as JavascriptExecutor).executeScript(script, element) as Boolean

}

}

Scroll from a offset of origin (element) by given amountThe final scenario is used when you need to scroll only a portion of the screen,

and it is already inside the viewport.

This uses the “Scroll From” method again, but the viewport is designated instead

of an element. An offset is specified from the upper left corner of the

current viewport. After the origin point is determined,

the page will be scrolled by the provided delta x and delta y values.

Note that if the offset from the upper left corner of the viewport falls outside of the screen,

it will result in an exception.

Java

Python

CSharp

Ruby

JavaScript

Kotlin WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();View Complete Code

View on GitHubexamples/java/src/test/java/dev/selenium/actions_api/WheelTest.java

Copy

Closepackage dev.selenium.actions_api;

import dev.selenium.BaseChromeTest;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.interactions.WheelInput;

public class WheelTest extends BaseChromeTest {

@Test

public void shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

new Actions(driver)

.scrollToElement(iframe)

.perform();

Assertions.assertTrue(inViewport(iframe));

}

@Test

public void shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

int deltaY = footer.getRect().y;

new Actions(driver)

.scrollByAmount(0, deltaY)

.perform();

Assertions.assertTrue(inViewport(footer));

}

@Test

public void shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement iframe = driver.findElement(By.tagName("iframe"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");

WebElement footer = driver.findElement(By.tagName("footer"));

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);

new Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

@Test

public void shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html");

WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);

new Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform();

WebElement iframe = driver.findElement(By.tagName("iframe"));

driver.switchTo().frame(iframe);

WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));

Assertions.assertTrue(inViewport(checkbox));

}

private boolean inViewport(WebElement element) {

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

return (boolean) ((JavascriptExecutor) driver).executeScript(script, element);

}

}

scroll_origin = ScrollOrigin.from_viewport(10, 10)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()View Complete Code

View on GitHubexamples/python/tests/actions_api/test_wheel.py

Copy

Closefrom time import sleep

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.common.actions.wheel_input import ScrollOrigin

def test_can_scroll_to_element(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

ActionChains(driver)\

.scroll_to_element(iframe)\

.perform()

assert _in_viewport(driver, iframe)

def test_can_scroll_from_viewport_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

delta_y = footer.rect['y']

ActionChains(driver)\

.scroll_by_amount(0, delta_y)\

.perform()

sleep(0.5)

assert _in_viewport(driver, footer)

def test_can_scroll_from_element_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

iframe = driver.find_element(By.TAG_NAME, "iframe")

scroll_origin = ScrollOrigin.from_element(iframe)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_element_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

footer = driver.find_element(By.TAG_NAME, "footer")

scroll_origin = ScrollOrigin.from_element(footer, 0, -50)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def test_can_scroll_from_viewport_with_offset_by_amount(driver):

driver.get("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

scroll_origin = ScrollOrigin.from_viewport(10, 10)

ActionChains(driver)\

.scroll_from_origin(scroll_origin, 0, 200)\

.perform()

sleep(0.5)

iframe = driver.find_element(By.TAG_NAME, "iframe")

driver.switch_to.frame(iframe)

checkbox = driver.find_element(By.NAME, "scroll_checkbox")

assert _in_viewport(driver, checkbox)

def _in_viewport(driver, element):

script = (

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

"return f\n"

"window.pageYOffset&&t+o>window.pageXOffset"

)

return driver.execute_script(script, element)

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Viewport = true,

XOffset = 10,

YOffset = 10

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();View Complete Code

View on GitHubexamples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs

Copy

Closeusing System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;

using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI

{

[TestClass]

public class WheelTest : BaseChromeTest

{

[TestMethod]

public void ShouldAllowScrollingToAnElement()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

new Actions(driver)

.ScrollToElement(iframe)

.Perform();

Assert.IsTrue(IsInViewport(iframe));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

int deltaY = footer.Location.Y;

new Actions(driver)

.ScrollByAmount(0, deltaY)

.Perform();

Assert.IsTrue(IsInViewport(footer));

}

[TestMethod]

public void ShouldScrollFromElementByGivenAmount()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = iframe

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromElementByGivenAmountWithOffset()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html";

IWebElement footer = driver.FindElement(By.TagName("footer"));

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Element = footer,

XOffset = 0,

YOffset = -50

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

[TestMethod]

public void ShouldAllowScrollingFromViewportByGivenAmountFromOrigin()

{

driver.Url =

"https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html";

var scrollOrigin = new WheelInputDevice.ScrollOrigin

{

Viewport = true,

XOffset = 10,

YOffset = 10

};

new Actions(driver)

.ScrollFromOrigin(scrollOrigin, 0, 200)

.Perform();

IWebElement iframe = driver.FindElement(By.TagName("iframe"));

driver.SwitchTo().Frame(iframe);

IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox"));

Assert.IsTrue(IsInViewport(checkbox));

}

private bool IsInViewport(IWebElement element)

{

String script =

"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"

+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"

+ "return f\n"

+ "window.pageYOffset&&t+o>window.pageXOffset";

IJavaScriptExecutor javascriptDriver = this.driver as IJavaScriptExecutor;

return (bool)javascriptDriver.ExecuteScript(script, element);

}

}

} scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(10, 10)

driver.action

.scroll_from(scroll_origin, 0, 200)

.performView Complete Code

View on GitHubexamples/ruby/spec/actions_api/wheel_spec.rb

Copy

Close# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Scrolling' do

let(:driver) { start_session }

it 'scrolls to element' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

driver.action

.scroll_to(iframe)

.perform

expect(in_viewport?(iframe)).to eq true

end

it 'scrolls by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

delta_y = footer.rect.y

driver.action

.scroll_by(0, delta_y)

.perform

expect(in_viewport?(footer)).to eq true

end

it 'scrolls from element by given amount' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

iframe = driver.find_element(tag_name: 'iframe')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(iframe)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls from element by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')

footer = driver.find_element(tag_name: 'footer')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(footer, 0, -50)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

it 'scrolls by given amount with offset' do

driver.get('https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html')

scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.viewport(10, 10)

driver.action

.scroll_from(scroll_origin, 0, 200)

.perform

iframe = driver.find_element(tag_name: 'iframe')

driver.switch_to.frame(iframe)

checkbox = driver.find_element(name: 'scroll_checkbox')

expect(in_viewport?(checkbox)).to eq true

end

end

def in_viewport?(element)

in_viewport = <<~IN_VIEWPORT

for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;

e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;

return f

window.pageYOffset&&t+o>window.pageXOffset

IN_VIEWPORT

driver.execute_script(in_viewport, element)

end

await driver.actions()

.scroll(10, 10, 0, 200)

.perform()

View Complete Code

View on GitHubexamples/javascript/test/actionsApi/wheelTest.spec.js

Copy

Closeconst { By, Browser, Builder} = require('selenium-webdriver')

const assert = require('assert')

describe('Actions API - Wheel Tests', function () {

let driver

before(async function () {

driver = await new Builder().forBrowser('chrome').build();

})

after(async() => await driver.quit())

it('Scroll to element', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 0, iframe)

.perform()

assert.ok(await inViewport(iframe))

})

it('Scroll by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const footer = await driver.findElement(By.css("footer"))

const deltaY = (await footer.getRect()).y

await driver.actions()

.scroll(0, 0, 0, deltaY)

.perform()

await driver.sleep(500)

assert.ok(await inViewport(footer))

})

it('Scroll from an element by a given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(0, 0, 0, 200, iframe)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an element with an offset', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

const iframe = await driver.findElement(By.css("iframe"))

const footer = await driver.findElement(By.css("footer"))

await driver.actions()

.scroll(0, -50, 0, 200, footer)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

it('Scroll from an offset of origin (element) by given amount', async function () {

await driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

const iframe = await driver.findElement(By.css("iframe"))

await driver.actions()

.scroll(10, 10, 0, 200)

.perform()

await driver.sleep(500)

await driver.switchTo().frame(iframe)

const checkbox = await driver.findElement(By.name('scroll_checkbox'))

assert.ok(await inViewport(checkbox))

})

function inViewport(element) {

return driver.executeScript("for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset", element)

}

}) val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()View Complete Code

View on GitHubexamples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt

Copy

Closepackage dev.selenium.actions_api

import dev.selenium.BaseTest

import org.junit.jupiter.api.Assertions

import org.junit.jupiter.api.Test

import org.openqa.selenium.By

import org.openqa.selenium.JavascriptExecutor

import org.openqa.selenium.WebElement

import org.openqa.selenium.interactions.Actions

import org.openqa.selenium.interactions.WheelInput

class WheelTest : BaseTest() {

@Test

fun shouldScrollToElement() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

Actions(driver)

.scrollToElement(iframe)

.perform()

Assertions.assertTrue(inViewport(iframe))

}

@Test

fun shouldScrollFromViewportByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val deltaY = footer.getRect().y

Actions(driver)

.scrollByAmount(0, deltaY)

.perform()

Assertions.assertTrue(inViewport(footer))

}

@Test

fun shouldScrollFromElementByGivenAmount() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val iframe = driver.findElement(By.tagName("iframe"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromElementByGivenAmountWithOffset() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

val footer = driver.findElement(By.tagName("footer"))

val scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50)

Actions(driver)

.scrollFromOrigin(scrollOrigin,0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

@Test

fun shouldScrollFromViewportByGivenAmountFromOrigin() {

driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10)

Actions(driver)

.scrollFromOrigin(scrollOrigin, 0, 200)

.perform()

val iframe = driver.findElement(By.tagName("iframe"))

driver.switchTo().frame(iframe)

val checkbox = driver.findElement(By.name("scroll_checkbox"))

Assertions.assertTrue(inViewport(checkbox))

}

fun inViewport(element: WebElement): Boolean {

val script = "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f\nwindow.pageYOffset&&t+o>window.pageXOffset"

return (driver as JavascriptExecutor).executeScript(script, element) as Boolean

}

}

最后修改 August 4, 2024: refactor[js]: update code samples to use mocha and update docs (4e38f3fbcdd)