Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Selenium向下滚动?_Java_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Java 如何使用Selenium向下滚动?

Java 如何使用Selenium向下滚动?,java,selenium,xpath,css-selectors,webdriverwait,Java,Selenium,Xpath,Css Selectors,Webdriverwait,在第页中,我需要单击链接隐私政策(它会打开一个新对话框),我必须使用Selenium和Java向下滚动 以下是我尝试过的: WebElement element = driver.findElement(By.xpath("/html/body/div[2]")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); 它不起作用它滚动背景页面而不是活动对话框。您可

在第页中,我需要单击链接隐私政策(它会打开一个新对话框),我必须使用Selenium和Java向下滚动

以下是我尝试过的:

WebElement element = driver.findElement(By.xpath("/html/body/div[2]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

它不起作用它滚动背景页面而不是活动对话框。

您可以滚动到相对于平面中像素数的位置:

JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");

要滚动的
div
有一个唯一的id,您需要首先获取对该id的引用:

WebElement element = driver.findElement(By.id('document-content'));
之后,您可以获取该隐私策略div中的最后一个孩子:

List<WebElement> children = element.findElements(By.tagName("div"));
assertTrue(children.size() > 0);
WebElement elementToScrollTo = children.get(children.size()-1);
List children=element.findElements(按.tagName(“div”));
assertTrue(children.size()>0);
WebElement elementToScrollTo=children.get(children.size()-1);
现在,您可以滚动到
elementToScrollTo
以调用链接上的
click()
,其中包含文本隐私政策您只需诱导WebDriverWait,即可单击所需元素,然后再次引导WebDriverWait使所需元素可见,然后滚动到视图中,您可以使用以下解决方案:

  • 代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class insly_Privacy_Policy {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver=new FirefoxDriver();
            driver.get("https://signup.insly.com/signup");
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("privacy policy"))).click();
            WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//div[@id='document-content']//following::div[contains(.,'Revision')]"))));
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
        }
    }
    
  • 浏览器快照:


+1,但您可以将代码简化为
WebElement=driver.findElement(By.xpath(“//div[@id='document-content']/*[last()”);((JavascriptExecutor)driver.executeScript(“参数[0]。ScrollingToView(true);”,元素)
@kamyarastesh,还要确保您已经添加了一个等待模式窗口来显示
WebDriverWait(driver,5)。直到(ExpectedConditions.visibilityOfElementLocated(By.id(“文档内容”))
@Andersson请查找此问题谢谢您的回复,让我消化一下您的答案,我会更新我的问题。当然,我将在这里添加链接,以确保您看到我的新问题。提前谢谢。请在DebanjanB找到这个问题。您有移动浏览器滚动的解决方案吗?我尝试过may的解决方案,但它不起作用。@AlImran如果你能在我们社区的移动浏览器滚动上提出一个指定的问题,有多少贡献者可以帮助你。