Jainish Kapadia
Jainish Kapadia

Reputation: 2611

How to do horizontal scroll using selenium webdriver

I want to perform horizontal scroll using on this site

1) First reduce the browser size until you will see table scroll bar.

I have done with following code snippet, but it didn't work for me well.

driver.get("https://weather.com/en-IN/weather/5day/l/USID0011:1:US");
Dimension d = new Dimension(521,628);
driver.manage().window().setSize(d);
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

My code is working well till now, but my execution is getting stopped from here.

I have try this scrolling issue with Action class but, same issue is getting replicated.

Actions act = new Actions(driver);
WebElement horizontal_scroll = driver.findElement(By.xpath("//div[@id='twc-scrollabe']/table"));
act.clickAndHold(horizontal_scroll).moveByOffset(40, 0).release().perform();

I have try with this below method also, but same issue is getting replicated.

WebElement horizontal_scroll = driver.findElement(By.xpath("//div[@id='twc-scrollabe']/table"));
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript(driver.execute_script("arguments[0].scrollIntoView()", horizontal_scroll));

In my application, I can not go with this method because there no style attribute is available inside table tag.

JavascriptExecutor jse = (JavascriptExecutor) driver;     
jse.executeScript("document.getElementById(''twc-scrollabe').setAttribute('style', 'position: relative; top: 0px; left: -658px; width: 1461px;')");

Can anyone help me on this issue?
Your help must be appreciated.
Best Regards,
Jainish Kapadia

Upvotes: 0

Views: 26557

Answers (3)

sagarp
sagarp

Reputation: 81

(JavascriptExecutor) driver).executeScript("arguments[0].scrollLeft = arguments[0].offsetWidth", element);

Try above to scroll on extreme right - it worked for me. If you want to scroll on left change accordingly.

Upvotes: 0

Norayr Sargsyan
Norayr Sargsyan

Reputation: 1868

This works for both horizontal and vertical scroll if you need to scroll to the particular element

public void scrollToElement(WebElement element) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) getDriver();
        javascriptExecutor.executeScript("arguments[0].scrollIntoView(true);", element);
    }

Upvotes: 0

Andersson
Andersson

Reputation: 52695

Simply use below for horizontal scroll

JavascriptExecutor jse = (JavascriptExecutor) driver;     
jse.executeScript("document.querySelector('table th:last-child').scrollIntoView();");

This should allow to scroll to the last column of the table

Upvotes: 3

Related Questions