【问题标题】:Scroll a div on a webpage without scrolling the main page in Selenium Webdriver在 Selenium Webdriver 中滚动网页上的 div 而不滚动主页
【发布时间】:2017-09-18 02:10:34
【问题描述】:

如何使用 Selenium Webdriver 在 Firefox 上处理嵌套滚动条?我试图自动化的功能是在向下滚动整个框架时,启用下一步按钮。我尝试了 javascript 执行程序,但它滚动主页而不是 div 内。我也使用 Actions 进行了尝试,请参阅下面的代码:

WebElement snapshot_list = driver.findElement(By.id("snapshots-list"));
Actions scrolldown = new Actions(driver);

scrolldown.moveToElement(snapshot_list).build().perform();
snapshot_list.click();

scrolldown.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

它不会导致任何错误,测试用例在 TestNG 中显示为 Passed 但它只是跳过了 scrolldown.keyDown 部分。我究竟做错了什么?任何帮助将不胜感激。

【问题讨论】:

  • 为什么需要scrolldown.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform(); part?

标签: javascript java selenium selenium-webdriver automation


【解决方案1】:

下面的代码可以让我滚动 div-

WebElement eleAssistanceInput = driver.findElement(By.id("abc"));
//the above element will be visible after scrolling down the div.                       
                        ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", eleAssistanceInput);

                        eleAssistanceInput.click();

【讨论】:

    【解决方案2】:

    您已经提到您尝试过 Javascript Executor,但不知道您尝试了哪些。如果您已经在下面尝试过,请忽略它们。

    (((JavascriptExecutor)driver)).executeScript("document.getElementById('snapshots-list').scrollIntoView({ 
      behavior: 'smooth' 
    });");
    

    (((JavascriptExecutor)driver)).executeScript("document.getElementById('snapshots-list').scrollIntoView(true);");
    

    (((JavascriptExecutor)driver)).executeScript("jQuery(\"snapshots-list\").mouseover();");
    

    或如下所述移动到元素后立即单击

    WebElement snapshot_list = driver.findElement(By.id("snapshots-list"));
    Actions scrolldown = new Actions(driver);
    
    scrolldown.moveToElement(snapshot_list).click().build().perform();
    

    【讨论】:

    • 谢谢!我通过 Actions 做了一些调整,并且成功了。
    【解决方案3】:

    问题解决了!我通过 Actions 实现了它,我将一个元素集中在框架中,并在 while 循环中添加了以下条件:

    Actions action = new Actions(driver);
    action.moveToElement(FirstSnapshot).build().perform();
    FirstSnapshot.click();
    
    while(Nextbutton.isEnabled()== false)
            {
            action.keyDown(Keys.CONTROL).sendKeys(Keys.DOWN).perform();
            }
    System.out.println("Button is enabled");
    

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 2014-11-10
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多