如何使用java水平滚动窗口内的滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35458415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:41:23  来源:igfitidea点击:

how to scroll scrollbar horizontally which is inside a window using java

javaselenium

提问by Srikanth

My problem is to scroll the scroll bar horizontally which is inside window I used this code but it scroll the windows horizontal bar not that exactly scroll bar inside that window.

我的问题是水平滚动窗口内的滚动条我使用了此代码,但它滚动窗口水平条而不是该窗口内的滚动条。

    WebElement scroll = driver.findElement(By.xpath("//div[@id='gvLocationHorizontalRail']"));
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("window.scrollBy(250,0)", "");

回答by Ferrybig

You are using javascript that scrolls the main window, if you want to scroll a element, you should first get the element by id, then change the its scrollLeftproperty:

您正在使用滚动主窗口的 javascript,如果要滚动元素,您应该首先通过 id 获取元素,然后更改其scrollLeft属性:

JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript(
    "document.getElementById('gvLocationHorizontalRail').scrollLeft += 250", "");

If you instead want to change the scrollbar that moves up and down, you should change the scrollTopproperty.

如果您想更改上下移动的滚动条,则应更改该scrollTop属性。

回答by Pankaj Kumar Katiyar

In addition to answer by Ferrybig, can you try this:

WebElement scroll = driver.findElement(By.xpath("//div[@id='gvLocationHorizontalRail']"));
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript(
driver.execute_script("arguments[0].scrollIntoView()", scroll);

回答by Abhinav R

Use the following:

使用以下内容:

WebElement problematicElement= driver.findElement(By.xpath("//div[@id='blah']"));
(JavascriptExecutor)driver.executeScript("arguments[0].scrollIntoView()", problematicElement);

回答by Sulteric

You don't need to use Javascript to do this. I was having issues with solutions working the second, third and fourth times the page was loaded. The code below works and works always without error. It moves a scroll bar to the right. I broke it out because I prefer to see each action by itself for readability.

您不需要使用 Javascript 来执行此操作。我在第二次、第三次和第四次加载页面时遇到了解决方案的问题。下面的代码可以正常工作并且始终没有错误。它将滚动条向右移动。我打破了它,因为我更喜欢单独查看每个动作以提高可读性。

            myElement = (new WebDriverWait(driver, 30))
                    .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".ngscroll-scrollbar")));
            myElement.click();
            Actions move = new Actions(driver);
            move.moveToElement(myElement).clickAndHold();
            move.moveByOffset(125,0);
            move.release();
            move.perform();

回答by CarolCiola

In my case I could not find the horizontal element of a table, so another (bearish, but still) way to perform this action is:

在我的情况下,我找不到表格的水平元素,因此执行此操作的另一种(看跌但仍然)方法是:

        IWebElement yourElement = driver.FindElement(By.Id("id_XYZ"));
        yourElement.Click();

        int i = 0;
        while (i < 20)
        {
            yourElement.SendKeys(Keys.Right);
            ++i;
        }