自动上传 Blueimp 文件 - 获取 ElementNotVisibleException:元素当前不可见,因此可能无法与之交互

Automate Blueimp file upload - getting ElementNotVisibleException: Element is not currently visible and so may not be interacted with

我正在尝试自动上传 Blueimp 文件。但不断收到 ElementNotVisible 异常。

WebElement fileUpload = driver.findElement(By.xpath("//input[@type='file']"));
String imagePath = "image.png";
fileUpload.sendKeys(imagePath);

我尝试启用使用:

((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", fileUpload);`

但还是没有运气..

解决它的一个选择是使元素可见:

WebElement fileUpload = driver.findElement(By.xpath("//input[@type='file']"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block'; arguments[0].style.visibility = 'visible';", fileUpload);

String imagePath = "image.png";
fileUpload.sendKeys(imagePath);

另请参阅:

  • How to create javascript executor to make element visible in selenium webdriver
  • Selenium Webdriver - click on hidden elements