【问题标题】:why cannot i upload an image to an upload input button using selenium? It works fine when opened manually but why doesn't it work through selenium?为什么我不能使用 selenium 将图像上传到上传输入按钮?手动打开时它工作正常,但为什么它不能通过 selenium 工作?
【发布时间】:2020-06-09 11:01:48
【问题描述】:

我有一个要求上传图片的输入按钮。 html 代码如下所示:

<input type="file" name="files" data-file="" data-url="../upload/student/image" accept="image/*">

由于按钮打开了一个窗口屏幕,我们可以从中上传图像,我也尝试使用机器人类,但按钮没有以某种方式被点击。

WebElement upload= driver.findElement(By.xpath("//*[@id=\"student_image\"]/input[1]"));
    upload.click();
    Robot robot= new Robot();
    StringSelection string= new 
    StringSelection("C:\\Users\\pradi\\OneDrive\\Desktop\\outline\\RHEST\\avatar.jpg");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(string,null);
    robot.setAutoDelay(1000);
    Ctrl+V(Pressed)
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    Ctrl+V(Released)
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    robot.setAutoDelay(2000);
    //Press Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

显示错误:

org.openqa.selenium.InvalidArgumentException: invalid argument
  (Session info: chrome=83.0.4103.61)
Session ID: 28dd9e0fbe4ec0e77ba63ac627a7696f at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at student.student_form.adding_student(student_form.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

为了找出我的代码停止工作的确切点,我在每一行都尝试了 println,并发现从这一行 upload.click(); 代码不起作用 我正在使用 chrome 版本 83.0.4103.61

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    代替;

    upload.click();
    

    使用;

    uploadElement.sendKeys("path_to_image//example_image.png");
    

    【讨论】:

    • 是的,我也尝试过。当我使用它时,它进一步移动而没有显示任何错误,但没有显示任何结果。没有发生任何事情。
    【解决方案2】:

    你可以加入等待

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"student_image\"]/input[1]"))
    .sendKeys(C:\\Users\\pradi\\OneDrive\\Desktop\\outline\\RHEST\\avatar.jpg);
    

    请确认 xpath 和是否有 iframe

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2021-02-01
      • 1970-01-01
      相关资源
      最近更新 更多