Ações de Roda de Rolagem
Existem 5 cenários para rolagem em uma página.
Rolagem até o Elemento
Este é o cenário mais comum. Diferentemente dos métodos tradicionais de clique e envio de teclas, a classe de ações não rolará automaticamente o elemento de destino para a visualização, portanto, este método precisará ser usado se os elementos não estiverem dentro da janela de visualização.
Este método recebe um elemento da web como único argumento.
Independentemente de o elemento estar acima ou abaixo da tela de visualização atual, a janela de visualização será rolada de forma que a parte inferior do elemento esteja na parte inferior da tela.
WebElement iframe = driver.findElement(By.tagName("iframe")); new Actions(driver) .scrollToElement(iframe) .perform();
examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/python/tests/actions_api/test_wheel.py
from 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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();
examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs
using 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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) .perform
examples/ruby/spec/actions_api/wheel_spec.rb
# 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+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> 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()
examples/javascript/test/actionsApi/wheelTest.spec.js
const { 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset", element) } })
val iframe = driver.findElement(By.tagName("iframe")) Actions(driver) .scrollToElement(iframe) .perform()
examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" return (driver as JavascriptExecutor).executeScript(script, element) as Boolean } }
Rolar por uma Quantidade Especificada
Este é o segundo cenário mais comum para a rolagem. Passe um valor delta x e um valor delta y para o quanto rolar nas direções direita e para baixo. Valores negativos representam esquerda e para cima, respectivamente.
WebElement footer = driver.findElement(By.tagName("footer")); int deltaY = footer.getRect().y; new Actions(driver) .scrollByAmount(0, deltaY) .perform();
examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/python/tests/actions_api/test_wheel.py
from 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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();
examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs
using 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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) .perform
examples/ruby/spec/actions_api/wheel_spec.rb
# 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+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> 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()
examples/javascript/test/actionsApi/wheelTest.spec.js
const { 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" return (driver as JavascriptExecutor).executeScript(script, element) as Boolean } }
Rolagem a partir de um Elemento por uma Quantidade Especificada"
Este cenário é efetivamente uma combinação dos dois métodos mencionados anteriormente.
Para executar isso, use o método “Rolar a Partir de”, que recebe 3 argumentos. O primeiro representa o ponto de origem, que designamos como o elemento, e os dois seguintes são os valores delta x e delta y.
Se o elemento estiver fora da janela de visualização, ele será rolado para a parte inferior da tela e, em seguida, a página será rolada pelos valores delta x e delta y fornecidos.
WebElement iframe = driver.findElement(By.tagName("iframe")); WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe); new Actions(driver) .scrollFromOrigin(scrollOrigin, 0, 200) .perform();
examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/python/tests/actions_api/test_wheel.py
from 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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();
examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs
using 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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) .perform
examples/ruby/spec/actions_api/wheel_spec.rb
# 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+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> 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()
examples/javascript/test/actionsApi/wheelTest.spec.js
const { 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" return (driver as JavascriptExecutor).executeScript(script, element) as Boolean } }
Rolagem a partir de um Elemento com um Deslocamento
Este cenário é usado quando você precisa rolar apenas uma parte da tela que está fora da janela de visualização ou dentro da janela de visualização, mas a parte da tela que deve ser rolada está a uma distância conhecida de um elemento específico.
Isso utiliza novamente o método “Rolar a Partir”, e além de especificar o elemento, é especificado um deslocamento para indicar o ponto de origem da rolagem. O deslocamento é calculado a partir do centro do elemento fornecido.
Se o elemento estiver fora da janela de visualização, primeiro ele será rolado até a parte inferior da tela. Em seguida, a origem da rolagem será determinada adicionando o deslocamento às coordenadas do centro do elemento, e, finalmente, a página será rolada pelos valores delta x e delta y fornecidos.
Observe que se o deslocamento a partir do centro do elemento estiver fora da janela de visualização, isso resultará em uma exceção.
WebElement footer = driver.findElement(By.tagName("footer")); WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50); new Actions(driver) .scrollFromOrigin(scrollOrigin,0, 200) .perform();
examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/python/tests/actions_api/test_wheel.py
from 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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();
examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs
using 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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') scroll_origin = Selenium::WebDriver::WheelActions::ScrollOrigin.element(footer, 0, -50) driver.action .scroll_from(scroll_origin, 0, 200) .perform
examples/ruby/spec/actions_api/wheel_spec.rb
# 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+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> window.pageYOffset&&t+o>window.pageXOffset IN_VIEWPORT driver.execute_script(in_viewport, element) end
const footer = await driver.findElement(By.css("footer")) await driver.actions() .scroll(0, -50, 0, 200, footer) .perform()
examples/javascript/test/actionsApi/wheelTest.spec.js
const { 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" return (driver as JavascriptExecutor).executeScript(script, element) as Boolean } }
Rolar a partir de um Deslocamento de Origem (Elemento) por uma Quantidade Especificada
O cenário final é usado quando você precisa rolar apenas uma parte da tela que já está dentro da janela de visualização.
Isso utiliza novamente o método “Rolar a Partir”, mas a janela de visualização é designada em vez de um elemento. Um deslocamento é especificado a partir do canto superior esquerdo da janela de visualização atual. Após determinar o ponto de origem, a página será rolada pelos valores delta x e delta y fornecidos.
Observe que se o deslocamento a partir do canto superior esquerdo da janela de visualização sair da tela, isso resultará em uma exceção.
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10); new Actions(driver) .scrollFromOrigin(scrollOrigin, 0, 200) .perform();
examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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()
examples/python/tests/actions_api/test_wheel.py
from 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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();
examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs
using 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\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) .perform
examples/ruby/spec/actions_api/wheel_spec.rb
# 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+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> window.pageYOffset&&t+o>window.pageXOffset IN_VIEWPORT driver.execute_script(in_viewport, element) end
await driver.actions() .scroll(10, 10, 0, 200) .perform()
examples/javascript/test/actionsApi/wheelTest.spec.js
const { 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset", element) } })
val scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10) Actions(driver) .scrollFromOrigin(scrollOrigin, 0, 200) .perform()
examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt
package 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<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" return (driver as JavascriptExecutor).executeScript(script, element) as Boolean } }