【问题标题】:clicking the download button is not working properly in selenium java在 selenium java 中单击下载按钮无法正常工作
【发布时间】:2022-01-21 13:30:25
【问题描述】:

我没有遇到任何错误,因为没有找到任何元素,但是我的测试用例在控制台中传递,但是当我检查下载文件夹时,它显示了一些临时文件而不是实际的图像文件。如果有人解决了这个问题,那将非常有用。

driver.get("https://demoqa.com/elements");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            
            // Here normal 'findElement' is not working, hence used the javascript executor
            
            WebElement leftmenu = driver.findElement(By.xpath("(//li[@id='item-7']//span)[1]"));
            JavascriptExecutor executor = (JavascriptExecutor)driver;
            executor.executeScript("arguments[0].click();", leftmenu);  //clicking the left menu 
            
            Thread.sleep(5000);
            
            driver.findElement(By.xpath("//a[@download='sampleFile.jpeg']")).click();  // download button

【问题讨论】:

    标签: java selenium selenium-webdriver testng cucumber-java


    【解决方案1】:

    您正在完成测试运行并在单击下载按钮后立即关闭浏览器。
    点击后尝试添加一个简单的睡眠。
    此外,您不应该使用这样的硬编码暂停

    Thread.sleep(5000);
    

    应改为使用显式等待。
    也请尝试等到元素可见,正如我在这里写的那样。我认为这会让您使用常规驱动程序.click() 方法单击它。
    试试这个:

    driver.get("https://demoqa.com/elements");
    
    driver.manage().window().maximize();
    WebDriverWait wait = new WebDriverWait(driver, globalDelay);
                
    // Here normal 'findElement' is not working, hence used the javascript executor
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//li[@id='item-7']//span)[1]"))).click();            
    //WebElement leftmenu = driver.findElement(By.xpath("(//li[@id='item-7']//span)[1]"));
    //JavascriptExecutor executor = (JavascriptExecutor)driver;
    //executor.executeScript("arguments[0].click();", leftmenu);  //clicking the left menu 
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@download='sampleFile.jpeg']"))).click();            
    
    Thread.sleep(5000);
    
    
    

    【讨论】:

      【解决方案2】:

      到元素下载上的click(),您可以使用以下Locator Strategies

      • 链接文本

        driver.findElement(By.linkText("Download")).click();
        
      • cssSelector

        driver.findElement(By.cssSelector("a#downloadButton[download^='sampleFile']")).click();
        
      • xpath

        driver.findElement(By.xpath("//a[@id='downloadButton' and starts-with(@download, 'sampleFile')]")).click();
        

      理想情况下,在需要为elementToBeClickable() 诱导WebDriverWait 的元素上click(),您可以使用以下任一Locator Strategies

      • 链接文本

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Download"))).click();
        
      • cssSelector

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a#downloadButton[download^='sampleFile']"))).click();
        
      • xpath

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='downloadButton' and starts-with(@download, 'sampleFile')]"))).click();
        

      PS:一旦您点击下载,不要立即关闭网络浏览器并等待一段时间以完成下载过程。

      【讨论】:

      • 非常感谢!!!它工作正常。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2013-11-11
      相关资源
      最近更新 更多