简体   繁体   English

上传照片按钮在 Selenium Webdriver 中不起作用

[英]Upload photo button not working in Selenium Webdriver

Upload photo button not working in Selenium Webdriver上传照片按钮在 Selenium Webdriver 中不起作用

What I have already tired我已经厌倦了什么

driver.findElement(uploadPhotoBtn).sendKeys("E:\\photo.png");

Also tried the Robot function还尝试了Robot function

    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);

Same Robot function working for another upload button, but while trying to use here, the .click not working that is why unable to use the Robot function.同一个Robot function 为另一个上传按钮工作,但在尝试使用此处时, .click不起作用,这就是无法使用Robot function 的原因。

HTML page source: 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>

Console log:控制台日志:

org.openqa.selenium.WebDriverException: unknown error: Element... is not clickable at point (314, 477). org.openqa.selenium.WebDriverException:未知错误:元素...在点 (314、477) 不可单击。 Other element would receive the click: (Session info: chrome=66.0.3359.181) (Driver info: chromedriver=2.35.528161其他元素将收到点击:(会话信息:chrome=66.0.3359.181)(驱动程序信息:chromedriver=2.35.528161

This is example of how to add file to upload button, don't quite sure what is path of button, but You have to find element with <input type="file"> element and interact with it: 这是如何添加文件到上传按钮的示例,不太确定什么是按钮的路径,但您必须找到带有<input type="file">元素的元素并与之交互:

WebElement uploadPhotoBtn = driver.find(By...); //type="file" 

File file = new File(path);
uploadPhotoBtn.sendKeys(file.getAbsolutePath());

...and this is how to get source, if You have some kind of preview ...如果您有某种预览,这就是获取源代码的方法

elementPreview.findElement(By.tagName("img")).getAttribute("src");

Hope this helps, 希望这可以帮助,

A couple of things: 有几件事:

1) The "Element is not clickable" error means that the upload button is covered somehow. 1) "Element is not clickable"错误意味着以某种方式覆盖上传按钮。 That could be it is disabled, behind some cover, or my favorite, the whole page clear div. 这可能是它被禁用,在一些封面后面,或者我最喜欢的,整个页面清除div。 Make sure that the button you're trying to click is truly available for clicking... 确保您尝试单击的按钮真正可用于单击...

2) For the .sendKeys() to work, you need to point to the <input type="file"> element. 2)要使.sendKeys()起作用,您需要指向<input type="file">元素。 Based on the variable name, you're trying to point to the <button> webelement instead. 根据变量名称,您尝试指向<button> webelement。

You need to ' type ' your file into the input element. 你需要“ 类型 ”您的文件到输入元素。 Your above send keys command isn't quite right. 您的上述发送键命令不太正确。

Code you can try: 您可以尝试的代码:

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

My answer was criticized at one SO post by @ JimEvans , who is a core contributor to the Selenium web automation framework . @ JimEvans在一篇SO帖子中批评了我的回答,他是Selenium 网络自动化框架的核心贡献者。 I learned something from him. 我从他身上学到了一些东西 This is what he had to say about upload button with <input type="file"> . 这是他不得不说的关于<input type="file">上传按钮。

If you are trying to upload a file, and the page in question uses the standard upload mechanisms provided by HTML, you can do this directly with Selenium itself. 如果您尝试上传文件,并且相关页面使用HTML提供的标准上传机制,您可以直接使用Selenium本身。 The standard HTML mechanism is with an <input type="file"> element. 标准HTML机制使用<input type="file">元素。 Once you've found that file upload element on the page, you can use element.sendKeys("full/path/and/file/name/here"); 一旦在页面上找到了文件上传元素,就可以使用element.sendKeys("full/path/and/file/name/here"); . This is documented in Step 10 of the algorithm for the Element Send Keys command of the W3C WebDriver Specification and is used in several file upload tests in the Selenium project's test code example . 在W3C WebDriver规范元素发送键命令的算法的步骤10中记录,并在Selenium项目的测试代码示例中用于多个文件上载测试。

As according the Error which you are getting, try to solve it by any of below, replacing click event: 根据您获得的错误,尝试通过以下任何一个解决它,替换click事件:

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

OR you can operate with JavaScript functionality, 或者您可以使用JavaScript功能,

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", "Your Webelement");
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM