0

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

5
  • I doubt your //div[@id='twc-scrollable']/table points at the scroller. If you have some specific element you should scroll towards, like a table cell, then you should find the cell and scrollIntoView() on it. NOT the table itself, since that is presumably already in view. Commented Feb 28, 2017 at 11:51
  • 1
    Just apply scrollIntoView to the last th element of the table... Commented Feb 28, 2017 at 11:53
  • @M.Prokhorov But scroll bar is inside the table and I can not able to locate only scroll bar. have you try this on your end? Commented Feb 28, 2017 at 11:54
  • Of course not, because I don't have your code. I can only infer from what I have seen in this question, and I can already tell, that you have selected a <table> element and act as if it's a scroll bar, which gets you this error. Commented Feb 28, 2017 at 11:57
  • @Andersson, Thnaks your hint is getting work for me :) Commented Feb 28, 2017 at 12:00

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have done with this code. WebElement horizontal_scroll = driver.findElement(By.xpath("//table[@class='twc-table']/..//following::th[@id='humidity']")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", horizontal_scroll);
Is there an equivalent in Python? I've a horizontal bar in my web page that I need to scroll so that other columns are visible.
0

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);
    }

Comments

0

(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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.