【问题标题】:Upload photo button not working in Selenium Webdriver上传照片按钮在 Selenium Webdriver 中不起作用
【发布时间】:2018-05-24 01:13:24
【问题描述】:

上传照片按钮在 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: 未知错误: Element ... is 在点 (314, 477) 不可点击。其他元素将收到 点击: (会话信息:chrome=66.0.3359.181)(驱动程序信息: chromedriver=2.35.528161

【问题讨论】:

  • 试试driver.findElement(By.name("image")).sendKeys("&lt;path/to/the/file&gt;")
  • 谢谢,我已经试过了 :),但是不行...
  • 在发布此问题之前,我已经尝试过此解决方案。机器人解决方案从那里应对。
  • @AlImran :当您手动单击按钮时,您是否必须向下滚动或执行任何操作?

标签: java selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

这是如何将文件添加到上传按钮的示例,不太确定按钮的路径是什么,但您必须找到带有&lt;input type="file"&gt; 元素的元素并与之交互:

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

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

...如果您有某种预览,这就是获取源代码的方法

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

希望对你有帮助,

【讨论】:

  • 谢谢它对我来说很好:) WebElement elem = driver.findElement(By.xpath("//input[@type='file']")); File file = new File(logoPath); elem.sendKeys(file.getAbsolutePath());
  • 很高兴我能帮上忙。
【解决方案2】:

有几点:

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

2) 要使.sendKeys() 起作用,您需要指向&lt;input type="file"&gt; 元素。根据变量名称,您正试图指向 &lt;button&gt; 网络元素。

【讨论】:

  • 1.元素是可点击的,因为我可以手动点击。我还使用了睡眠/隐式等待(等待可点击)来实现可点击。 2.By uploadPhotoBtn = By.xpath(""); driver.findElement(uploadPhotoBtn).sendKeys("E:\\photo.png");我已经用过,但是不能用。可以吗?
【解决方案3】:

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

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

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

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

【讨论】:

    【解决方案4】:

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

    您可以尝试的代码:

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

    【讨论】:

      【解决方案5】:

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

      如果您尝试上传文件,并且相关页面使用 HTML 提供的标准上传机制,您可以直接使用 Selenium 本身执行此操作。标准的 HTML 机制带有 &lt;input type="file"&gt; 元素。在页面上找到文件上传元素后,您可以使用element.sendKeys("full/path/and/file/name/here");。这在 Element Send Keys command of the W3C WebDriver Specification 算法的第 10 步中有记录,并在 Selenium 项目的测试代码 example 中的多个文件上传测试中使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多