【问题标题】:File upload using selenium Web driver?使用 selenium Web 驱动程序上传文件?
【发布时间】:2019-10-21 06:25:30
【问题描述】:
WebElement product_image = driver.findElement(By.id("pdt_upload_preview"));
System.out.println("-------Product image selected----------\n-----------------------");
product_image.click();  
product_image.sendKeys("C:\\TEST\\TEST\\Images\\Images(1).jpg");

我不能用这个上传文件。

【问题讨论】:

标签: selenium file upload


【解决方案1】:

希望不用点击,试试看

WebElement product_image = driver.findElement(By.id("pdt_upload_preview"));
product_image.sendKeys("C:\\TEST\\TEST\\Images\\Images(1).jpg");

【讨论】:

    【解决方案2】:

    如果您的代码不起作用,则可能是您的图片上传元素被隐藏了。您可以尝试执行 Javascript 来显示元素:

    WebElement product_image = driver.findElement(By.id("pdt_upload_preview"));
    
    // execute javascript to reveal the element
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript("arguments[0].style.display = 'block';", product_image);
    
    // send keys to the element
    product_image.sendKeys("C:\\TEST\\TEST\\Images\\Images(1).jpg");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      相关资源
      最近更新 更多