滚轮操作
在页面上滚动有 5 种情况.
滚动到元素
这是最常见的场景. 与传统的点击和发送按键的方法不同, 动作类不会自动将目标元素滚动到视图中, 因此如果元素不在视口内, 就需要使用此方法.
此方法仅将一个网页元素作为参数.
无论该元素是在当前视图屏幕的上方还是下方, 视口都会滚动, 使该元素的底部位于屏幕底部.
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
}
}
按给定的量进行滚动
这是第二种最常见的滚动场景. 传入一个 x 方向的偏移量和一个 y 方向的偏移量, 表示向右和向下滚动的距离. 负值分别表示向左和向上滚动.
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
}
}
将元素按照给定的量进行滚动
这种情形实际上是上述两种方法的结合.
要执行此操作, 请使用“从…滚动”方法,
该方法需要 3 个参数.
第一个参数表示起始点, 我们将其指定为元素,
后两个参数分别是 x 偏移量和 y 偏移量的值.
如果元素不在视口内, 它将滚动到屏幕底部, 然后页面将根据提供的 x 和 y 偏移量进行滚动.
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
}
}
从具有偏移量的元素进行滚动
当您需要滚动屏幕的某一部分, 而该部分位于视口之外, 或者位于视口之内但必须滚动的屏幕部分与特定元素之间存在已知偏移量时, 会使用此场景.
这再次使用了“从…滚动”的方法, 并且除了指定元素之外, 还指定了一个偏移量来表明滚动的起始点. 该偏移量是从所提供的元素的中心计算得出的.
如果元素不在视口内, 它首先会被滚动到屏幕底部, 然后滚动的原点将通过将偏移量添加到元素中心的坐标来确定, 最后页面将根据提供的 x 和 y 偏移量值进行滚动.
请注意, 如果元素中心的偏移量超出视口范围, 将会导致异常.
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);
}
}
} 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')/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.sleep(500)
await driver.switchTo().frame(iframe)
const checkbox = await driver.findElement(By.name('scroll_checkbox'))
/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
}
}
从给定元素的原点偏移量处滚动指定的距离
最后一种情况适用于您需要滚动屏幕的某一部分, 而该部分已处于视口内的情况.
这再次使用了“从…滚动”的方法, 但这次指定的是视口而非某个元素. 从当前视口的左上角开始指定偏移量. 确定原点后, 页面将根据提供的 x 偏移量和 y 偏移量进行滚动.
请注意, 如果从视口左上角算起的偏移量超出屏幕范围, 将会导致异常.
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
}
}




