代码之家  ›  专栏  ›  技术社区  ›  Al Imran

“上载照片”按钮在Selenium WebDriver中不起作用

  •  1
  • Al Imran  · 技术社区  · 7 年前

    “上载照片”按钮在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);
    

    同样 机器人 函数用于另一个上载按钮,但在尝试在此使用时, .click 不工作,所以不能使用 机器人 功能。

    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:未知错误:元素…是 点(314477)不可点击。其他元素将接收 点击: (会话信息:chrome=66.0.3359.181)(驱动程序信息: Chromedriver=2.35.528161

    5 回复  |  直到 7 年前
        1
  •  2
  •   Kovacic    7 年前

    这是如何将文件添加到“上载”按钮的示例,不太确定按钮的路径,但必须使用 <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");

    希望这有帮助,

        2
  •  1
  •   GPT14    7 年前

    一些事情:

    1) "Element is not clickable" 错误意味着上载按钮被某种方式覆盖。可能是它被禁用了,在某个封面后面,或者我最喜欢的,整个页面都被清除了。请确保您试图点击的按钮确实可以点击…

    2)对于 .sendKeys() 要工作,您需要指向 <input type="file"> 元素。根据变量名,您试图指向 <button> 而是WebElement。

        3
  •  0
  •   Ishita Shah Y_Sh    7 年前

    根据您得到的错误,尝试通过以下任一方法解决,替换Click事件:

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

    或者您可以使用JavaScript功能,

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].click();", "Your Webelement");
    
        4
  •  0
  •   cruisepandey    7 年前

    你需要 类型 '输入元素中的文件。上面的send keys命令不太正确。

    您可以尝试的代码:

    driver.findElement(By.xpath("//input[@name="image"]")).sendKeys("Image Path Here") ;
    
        5
  •  0
  •   Ratmir Asanov    7 年前

    我的回答在一篇这样的文章中受到了批评@ 吉米文斯 ,谁是 Web自动化框架 . 我从他那里学到了一些东西。这就是他说的关于上传按钮 <input type="file"> .

    如果您尝试上载文件,并且所讨论的页面使用HTML提供的标准上载机制,那么您可以直接使用Selenium本身来执行此操作。标准的HTML机制是 <input type=“文件”> 元素。一旦在页面上找到文件上载元素,就可以使用 element.sendKeys("full/path/and/file/name/here"); . 这在 Element Send Keys command of the W3C WebDriver Specification 并用于Selenium项目的测试代码中的多个文件上载测试。 example .