【问题标题】:how to fix selenium web driver is moving too fast?如何修复硒网络驱动程序移动太快?
【发布时间】:2016-02-23 10:49:51
【问题描述】:

没有什么是适合它的。建议我更好的选择。由于速度快,我无法以更好的方式检测执行。 这是我的代码:

public static void main(String[] args) {

    //**********************************open ff
        WebDriver driver=new FirefoxDriver();
    //**************************************maximize ff 
        driver.manage().window().maximize();

            Logger log = Logger.getLogger("devpinoyLogger");
        driver.get("http://navvitistgvm.cloudapp.net/nvrppluginassist/Account/Login");

            log.debug("entring username");      
        driver.findElement(By.xpath("//*[@id='UserName']")).sendKeys("rpadmin");

            log.debug("entering password");
        driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("Password123");

            log.debug("Clicking login");
        driver.findElement(By.xpath("//*[@id='loginForm']/form/div[4]/div/input")).click();

            log.debug("Clicking voucher");
        driver.findElement(By.xpath("html/body/nav/div[2]/div[2]/ul/li[2]/a")).click();



                log.debug("selecting search_voucher");

                 List<WebElement> elements=driver.findElements(By.id("VoucherType"));
                 //elements.get(0).click(); //GV
                 elements.get(1).click();  //GC
                 //elements.get(2).click();//AP

                 driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);

            driver.findElement(By.xpath("//*[@id='main']/form[2]/div[2]/input[4]")).click();
            driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//*[@id='main']/div[1]/span/a")).click();
    }
    }

没有什么是适合它的。建议我更好的选择。由于速度很快,我无法以更好的方式检测执行。

【问题讨论】:

  • 你是说你的程序太快了,问你怎么放慢它?
  • 您能否为您的问题添加更多细节,以便我更好地为您解答。你到底有什么问题?
  • 是的。因此,有时它会给出未找到此类元素的异常。
  • @cconolly slenium 网络驱动程序点击或快速输入每个细节。我需要放慢整个过程。
  • 您希望每条命令之间的运行时间更长吗?你说你没有得到这样的元素异常,表明测试运行有点太早而不是太快。哪些元素会出现此异常?

标签: javascript selenium selenium-webdriver


【解决方案1】:

对于 2020 年寻求帮助的你,我推荐这个页面: https://www.selenium.dev/documentation/en/webdriver/waits/

我使用了显式等待方法!帮了大忙

【讨论】:

    【解决方案2】:

    如果您在执行某些操作之后并在另一个操作之前没有收到此类元素异常,请使用显式等待 (http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp)。

    请不要添加一堆睡眠!!

    【讨论】:

    • 我已经尝试过你提供的链接。仍然无法正常工作。你能根据我的代码给我建议吗???
    • 似乎对点击的响应发生了一些变化,因此您需要等待点击事件后状态的一些变化。例如,如果凭证按钮/链接在您单击登录之前甚至不在 DOM 中,您必须等待它出现。
    【解决方案3】:

    所以这不是对您问题的直接答案,会减慢进程,但这是我认为您遇到的问题的答案。

    您不需要使这些步骤运行得更慢,但您需要确保在页面正确加载之前不运行任何步骤。

    您可以使用WebDriverWaitvisibilityOfElementLocated 来解决此问题。

    我在您的代码中添加了几行

    import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    
    public static void main(String[] args) {
    
      //**********************************open ff
      WebDriver driver = new FirefoxDriver();
      WebDriverWait wait = new WebDriverWait(driver, 30);
      //**************************************maximize ff 
    
      driver.manage().window().maximize();
    
      Logger log = Logger.getLogger("devpinoyLogger");
      driver.get("http://navvitistgvm.cloudapp.net/nvrppluginassist/Account/Login");
      // driver.get should block the execution of following steps untill the
      // page has loaded so this line below in theory shouldnt be needed.
      // Try it in your code and see for yourself
      // Wait till the username field is visible.
      wait.until(visibilityOfElementLocated(By.xpath("//*[@id='UserName']"))));
    
      log.debug("entring username");      
      driver.findElement(By.xpath("//*[@id='UserName']")).sendKeys("rpadmin");
    
      log.debug("entering password");
      driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("Password123");
    
      log.debug("Clicking login");
      driver.findElement(By.xpath("//*[@id='loginForm']/form/div[4]/div/input")).click();
    
      // I assume here is your other issue, when clixking login the view changes
      // and your running this next command before the view has properly refreshed.
      // add a wait here as well.
      wait.until(visibilityOfElementLocated(By.xpath("html/body/nav/div[2]/div[2]/ul/li[2]/a")));
    
      log.debug("Clicking voucher");
      driver.findElement(By.xpath("html/body/nav/div[2]/div[2]/ul/li[2]/a")).click();
    
      log.debug("selecting search_voucher");
    
       List<WebElement> elements=driver.findElements(By.id("VoucherType"));
       //elements.get(0).click(); //GV
       elements.get(1).click();  //GC
       //elements.get(2).click();//AP
    
       driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
    
      driver.findElement(By.xpath("//*[@id='main']/form[2]/div[2]/input[4]")).click();
      driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
          driver.findElement(By.xpath("//*[@id='main']/div[1]/span/a")).click();
    }
    }
    

    如果您真的希望代码执行“慢”一些我不推荐的东西(您做的有多慢?)更好的是使用我在上面向您展示的方法进行显式测试,并且只在您需要的地方添加等待知道您需要(例如,作为操作的结果加载页面/视图)

    一种方法是包装动作并添加超时,例如:

    public void waitAndClick(Xpath) {
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.findElement(By.xpath(Xpath)).click();
    }
    

    尝试第一种方法,它是一个更好的解决方案。

    【讨论】:

    • 是的 cconolly 我想减慢代码执行速度。你能帮我吗??
    • 更新了答案以举例说明如何做你想做的事,我仍然建议使用第一个解决方案,它会更好,并且会阻止你得到这些异常
    【解决方案4】:

    您需要使用 fluent 等待页面加载或在每次点击事件后等待特定元素加载,然后执行某些 selenium api 调用:

    您可以在此处找到有关 Fluent Wait of selenium 如何工作的文档 https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html

    【讨论】:

      【解决方案5】:

      如果隐式和显式不能按预期工作,您可以尝试 Thread.sleep(3000)(3 秒)。

      我希望下面的链接能阐明执行速度, setSpeed in Selenium WebDriver using Ruby

      【讨论】:

        【解决方案6】:

        我在自己的代码中遇到了同样的问题。我在我的应用程序中尝试了这些来自 selenium API 的代码:

        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
        wait.until(visibilityOfElementLocated(By.xpath(xpath))); 
        

        它工作得更好,但我仍然收到错误,例如 xpath 未找到。所以我发现 Selenium 的那些等待方法太快了,或者我的元素没有按时出现。

        现在有了这段代码,它对我的​​应用程序真的很好用:

        //extra waiting due to the limitiations of selenium, selenium sometimes loads too fast
                Random ran = new Random();
                //The integer x is now the random number that has a possible outcome of 800-1200.
                int x = ran.nextInt(401) + 800;
                try {
                    Thread.sleep(x);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-16
          • 2019-11-27
          • 1970-01-01
          • 1970-01-01
          • 2021-11-01
          相关资源
          最近更新 更多