英文:
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?
问题
我有一个输入按钮,需要上传图像。HTML 代码如下:
<input type="file" name="files" data-file="" data-url="../upload/student/image" accept="image/*">
由于这个按钮会打开一个窗口,用于上传图像,我还尝试使用机器人类(robot class),但是按钮似乎无法被点击。
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);
// Press Ctrl+V (Pressed)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Release 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)
...
为了找出代码在哪个位置停止工作,我在每一行代码中都尝试了 println,并发现从这一行开始代码停止工作:upload.click();
。
我使用的 Chrome 版本为 83.0.4103.61。
英文:
I have an input button that demands an image upload. The html code looks something like this:
<input type="file" name="files" data-file="" data-url="../upload/student/image" accept="image/*">
since the button opens a windows screen from where we can upload the image, i also tried using robot class but The button is not being clicked somehow.
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);
It is showing error:
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)
Just to find out the the exact point where my code stopped working i tried println in each lines, and found out that from this line upload.click();
the code wasn't working
I'm using chrome Version 83.0.4103.61
答案1
得分: 0
代替:
upload.click();
使用:
uploadElement.sendKeys("path_to_image//example_image.png");
英文:
Instead of;
upload.click();
Use;
uploadElement.sendKeys("path_to_image//example_image.png");
答案2
得分: 0
你可以加入等待
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。
英文:
You can join waiting
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);
Please confirm xpath and Is there an iframe
专注分享java语言的经验与见解,让所有开发者获益!
评论