Selenium webdriver 无法在selenium web驱动程序中上载文件,我已使用SendKeys()传递文件路径get upload,但仍然无法上载文件

Selenium webdriver 无法在selenium web驱动程序中上载文件,我已使用SendKeys()传递文件路径get upload,但仍然无法上载文件,selenium-webdriver,sendkeys,testng-eclipse,Selenium Webdriver,Sendkeys,Testng Eclipse,我正在尝试将一个文档从我的系统上载到一个应用程序,其中包括以下流程: 库页面>>上载文档按钮>>基于Web的选择文档弹出窗口>>单击弹出窗口中的选择文件后,将从需要选择文件的位置打开系统弹出窗口 对于上述情况,我编写的代码如下: WebElement=driver.findElementBy.xpath//button[@class='btn btn btn primary upload doc btn cap letter bold upload_document_image marginri

我正在尝试将一个文档从我的系统上载到一个应用程序,其中包括以下流程:

库页面>>上载文档按钮>>基于Web的选择文档弹出窗口>>单击弹出窗口中的选择文件后,将从需要选择文件的位置打开系统弹出窗口

对于上述情况,我编写的代码如下:

WebElement=driver.findElementBy.xpath//button[@class='btn btn btn primary upload doc btn cap letter bold upload_document_image marginrightfix18']; 动作动作=新动作驱动程序; action.moveToElementelement.click.perform

      WebDriverWait wait = new WebDriverWait(driver, 300);
      WebElement selectElement = wait.until(ExpectedConditions
              .visibilityOfElementLocated(By.xpath("//label[@class='btn btn-primary file-select-btn margintop10']")));

      WebElement selectdoc = driver.findElement(By.xpath("//label[@class='btn btn-primary file-select-btn margintop10']"));

      Actions actions = new Actions(driver);
      actions.moveToElement(selectdoc);
      actions.click();
//在此之前,单击“工作”并向我打开系统窗口,但使用发送键的文件的给定路径不工作,无法上载所述文件

      actions.sendKeys("C://Users//Vishnu//Documents//seleniumnotes.docx"); 
      actions.build().perform();
对于上面的代码,我遇到未知错误:无法聚焦元素错误,因为没有选择要上载的给定文件

请提出你有价值的答案。 如果使用.sendkeys方法获得答案,则效果良好

我也尝试过机器人类,但那对我来说也不起作用,所以尝试使用.sendkeys


你的回答可以救我一天

不能使用selenium操作窗口功能。selenium只能在浏览器上使用


因此,由于robot类不起作用,我认为您应该尝试使用Autoit软件上载文件

最后,我使用robot类在Actions的帮助下成功上载了文件

以下代码适用于我:

      StringSelection selection = new StringSelection("C:\\Users\\Vishnu\\Documents\\Chapter name.png");
      java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      clipboard.setContents(selection, 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);

若你们的文件上传按钮有输入标签,那个么你们可以使用下面的代码上传文件

WebElement ele = driver.findElement(By.LocStrgy("Locator with input tag"));
ele.sendKeys("path to file");

我用的是C,在你的问题中用的是同样的代码。我的问题是浏览窗口打开了,但没有选择文件,程序成功结束,没有任何错误