java - 如何在 Selenium Webdriver 中使用 "Upload Image Button"上传文件/照片

标签 java selenium selenium-webdriver

  WebElement UploadImg = driver.findElement(By.xpath("//*[@id='file_upload_button']"));
  UploadImg.click();
  WebElement frame =driver.switchTo().activeElement();
  frame.sendKeys("d:\5.jpg");

这段代码只是打开系统窗口,但它没有选择任何照片/文件

最佳答案

运行此代码:

JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].setAttribute('style', arguments[1])",` driver.findElement(By.xpath("//input[@type='file']")), "0");
js.executeScript("arguments[0].setAttribute('class', arguments[1])", driver.findElement(By.xpath("//input[@type='file']/../../div[2]")), "a");
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("Your Path to the file your system");

说明:每个浏览按钮都有一个 <input> DOM 中的标签处于隐藏状态。通过使用下面的代码行,我们只需更改包含 <input> 的标签的类和样式属性。标记,使其变得可见,并显示 sendKeys()可以对其执行命令。之后,当您执行 sendKeys 时与您要上传的图像/文件的绝对路径。

关于java - 如何在 Selenium Webdriver 中使用 "Upload Image Button"上传文件/照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025308/

相关文章:

ruby-on-rails - 选择:Capybara::Ambiguous:模糊匹配,找到 2 个匹配可见选项的元素

java - mysql浮点型

java - 如何在客户端/服务器 Java 应用程序中传输文件

java - SimpleXML 框架返回所有嵌套元素

java - 尽管不再有 WebElement,循环 List<WebElement> 仍不会停止

java - 获取 ul 中 li 元素的数量,而不存储在列表中

java - FlatMap 调用后,Reactor 不支持 runOn

javascript - 如何使用 Mechanize with Selenium 在 python 中单击按钮

python - 减少 pyinstaller exe 的大小

java - Selenium Webdriver submit() 与 click()