上传照片按钮在硒网络驱动程序中不起作用



上传照片按钮在Selenium网络驱动程序中不起作用

我已经厌倦了什么

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: 未知错误: Element ...是 在点 (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");

希望这有帮助,

有几件事:

1("Element is not clickable"错误意味着上传按钮以某种方式被覆盖。这可能是它被禁用,在一些封面后面,或者我最喜欢的,整个页面清除div。确保您尝试单击的按钮确实可用于单击...

2( 要使.sendKeys()正常工作,您需要指向<input type="file">元素。根据变量名称,您尝试改为指向<button>webelement。

根据您得到的错误,尝试通过以下任何一种来解决它,替换点击事件:

Actions act = new Actions(wd);
act.moveToElement("Your Webelement").click().perform();

或者你可以使用 JavaScript 功能进行操作,

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", "Your Webelement");

您需要将文件"键入"到输入元素中。您上面的发送密钥命令不太正确。

您可以尝试的代码:

driver.findElement(By.xpath("//input[@name="image"]")).sendKeys("Image Path Here") ;

我的回答在一篇SO帖子中受到@JimEvans的批评,他是Selenium网络自动化框架的核心贡献者。我从他身上学到了一些东西。这就是他对带有<input type="file">的上传按钮所说的话。

如果您尝试上传文件,并且相关页面使用 HTML 提供的标准上传机制,您可以直接使用 Selenium 本身执行此操作。标准的 HTML 机制带有<input type="file">元素。在页面上找到该文件上传元素后,您可以使用element.sendKeys("full/path/and/file/name/here");.这记录在 W3C WebDriver 规范的元素发送密钥命令的算法的步骤 10 中,并在 Selenium 项目的测试代码示例中的多个文件上传测试中使用。

WebElement upload = d.findElement(By.xpath("//*[@name='filename']"));
Thread.sleep(2000);
Actions action = new Actions(d);
action.moveToElement(upload);
action.click().build().perform();
Thread.sleep(2000);

相关内容

最新更新