【问题标题】:How to Scroll down with Selenium?如何使用 Selenium 向下滚动?
【发布时间】:2019-03-29 17:51:09
【问题描述】:

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

这是我尝试过的:

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

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

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要在 link 上使用文本 Privacy Policy 调用 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);
          }
      }
      
    • 浏览器快照:

    【讨论】:

    • 感谢您的回复,让我消化您的回答,我会更新我的问题。
    • 当然,我会在此处添加链接,以确保您看到我的新问题。提前致谢。
    • @DebanjanB 你有移动浏览器滚动的解决方案吗?我尝试了可能的解决方案,但它不起作用。
    • @AlImran 如果您可以在我们的社区中提出关于移动浏览器滚动的指定问题,粗略的贡献者可以帮助您。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您要滚动的div 有一个唯一的 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);
      

      您现在可以滚动到elementToScrollTo

      【讨论】:

      • +1,但您可以将代码简化为WebElement element = driver.findElement(By.xpath("//div[@id='document-content']/*[last()]")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
      • @KamyarParastesh ,还要确保您添加了等待模式窗口出现WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.id("document-content")))
      • @Andersson 请找到这个问题stackoverflow.com/questions/52986601/…
      猜你喜欢
      • 1970-01-01
      • 2013-04-14
      • 2020-08-31
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多