I am trying to scroll inside an element using the moveByOffset method of Actions class:
HTML code:
<html>
<head>
<meta charset="utf-8">
<style>
#container
{
width: 100px;
height: 100px;
border: 1px solid #ccc;
overflow-x: scroll;
}
#content
{
margin:4px, 4px;
padding:4px;
width: 500px;
height:500px;
text-align:justify;
background-color: #ccc;
}
</style>
</head>
<body>
<div id="container">
<div id="content">Click the button to slide right!</div>
</div>
<script>
const button = document.getElementById('slide');
button.onclick = function ()
{
document.getElementById('container').scrollLeft += 20;
};
</script>
</body>
</html>
Selenium code:
Actions ac = new Actions(driver);
// Scroll down
ac.clickAndHold(driver.findElement(By.cssSelector("div[id='container']"))).moveByOffset(0, 100).release().build().perform();
// Scroll up
ac.clickAndHold(driver.findElement(By.cssSelector("div[id='container']"))).moveByOffset(0, -100).release().build().perform();
// Scroll right
ac.clickAndHold(driver.findElement(By.cssSelector("div[id='container']"))).moveByOffset(100, 0).release().build().perform();
// Scroll left
ac.clickAndHold(driver.findElement(By.cssSelector("div[id='container']"))).moveByOffset(-100, 0).release().build().perform();
It is throwing MoveTargetOutOfBoundsException whenever we give negative value in the moveByOffset method.
Note: I know I can use javascript, but I want to do it using Actions class.
Please help.