java - 上传照片按钮在 Selenium Webdriver 中不起作用
问题描述
上传照片按钮在 Selenium Webdriver 中不起作用
我已经厌倦了什么
driver.findElement(uploadPhotoBtn).sendKeys("E:\\photo.png");
也试过这个Robot
功能
driver.findElement(uploadPhotoBtn).click();
StringSelection ss = new StringSelection(logoPath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
相同Robot
的功能适用于另一个上传按钮,但在此处尝试使用时,.click
不起作用,这就是无法使用该Robot
功能的原因。
HTML
页面来源:
> <div ng-show="!status.uploading" ng-class="{ '!isMobile':
> 'mewe-type-1' }" class="uploader-able !isMobile"><!-- ngIf: isMobile
> --><!-- ngIf: !isMobile --><button ng-if="!isMobile" class="btn-action radius ng-scope">Upload Photo</button><!-- end ngIf: !isMobile
> --><input capture="camera" accept="image/*" name="image" type="file" fileread="fileread" file="file" class="ng-isolate-scope"></div>
控制台日志:
org.openqa.selenium.WebDriverException:未知错误:元素 ... 在点 (314, 477) 处不可点击。其他元素将收到点击:(会话信息:chrome=66.0.3359.181)(驱动程序信息:chromedriver=2.35.528161
解决方案
这是如何将文件添加到上传按钮的示例,不太确定按钮的路径是什么,但是您必须找到带有元素的<input type="file">
元素并与之交互:
WebElement uploadPhotoBtn = driver.find(By...); //type="file"
File file = new File(path);
uploadPhotoBtn.sendKeys(file.getAbsolutePath());
...如果您有某种预览,这就是获取源代码的方法
elementPreview.findElement(By.tagName("img")).getAttribute("src");
希望这可以帮助,
推荐阅读
- javascript - lodash-es 中的意外令牌“导出”
- pine-script - 如何在 pinescript 中默认使绘图不可见
- html - li 元素内的一个 Div 改变位置
- postgresql - AWS RDS Proxy 可以直接与读取复制实例配对吗?
- jquery - toggleClass 仅在使用 .className 时工作
- java - 无法在 Angular + Spring Boot 项目中使用 *ngFor 显示元素列表
- node.js - 内容安全策略错误:后端页面无法在 Firefox 浏览器中加载
- mysql - AWS SSM 端口转发不起作用 - 使用最新的 SSM 代理
- python - 从文件离线更新 conda 环境
- c# - 避免不必要的字符串替换