0

嘿,我有一个关于硒的问题我有一个很长的表格,我想向下滚动一点以输入数据。我不希望它一直滚动到元素显示为止。

我使用了这段代码:

WebElement element = driver.findElement(locator);
        JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].scrollIntoView();", element);

它不起作用,因为硒滚动向下匹配,这是一种滚动到该元素的方式,因此我可以向它输入数据,例如滚动直到元素位于屏幕的模型中。如果滚动不查看问候,我不知道滚动查看的目的是什么

4

3 回答 3

1

If your usecase is to ...scroll a little bit down for entering data... you have multiple ways to achieve that:

  • You can induce WebDriverWait for the elementToBeClickable() which will automatically scroll the desired element within the Viewport as follows:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(driver.findElement(locator))).sendKeys("Bastian");
    
  • You can use Actions class method moveToElement() inconjunction with sendKeys() as well which will also automatically scroll the desired element within the Viewport as follows:

    new Actions(driver).moveToElement(driver.findElement(locator)).sendKeys("Bastian").build().perform();
    
  • You can still use scrollIntoView() method to scroll the element first and then induce WebDriverWait for the elementToBeClickable() as follows:

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(locator));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(driver.findElement(locator))).sendKeys("Bastian");
    
  • You can also use scrollBy() method to scroll down certain amount of xy-coord (adjusted) as follows and then locate the element:

    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(driver.findElement(locator))).sendKeys("Bastian");
    

References

You can find a couple of relevant discussions in:

于 2020-01-10T10:04:31.967 回答
0

这是我创造的,似乎对我有用。

for i in range(5000):
    browse = "window.scrollTo(0," + str(i) + ")"
    browser.execute_script(browse)
    i = i + 400

它几乎滚动得很慢,因此页面上的每个元素都已加载。

于 2020-08-19T14:39:29.887 回答
0

请试试这个。这个对我有用。

 public void scrollElementToCenter(WebElement element) {

        try {
            if (EnvironmentConstants.RUNNING_ON_CHROME) {
                ((JavascriptExecutor) driver)
                        .executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", element);
            } else {
                ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);
            }
        } catch (UnsupportedOperationException exception) {
            //LOGGER.error("UnsupportedOperationException occurred : " + exception.getMessage(), exception);
        }
    }

对于Chrome浏览器,您可以使用它

((JavascriptExecutor) driver)
                        .executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", element);
于 2020-01-10T10:42:11.557 回答