Java 如何使用selinium web驱动程序上载文件?
我正在尝试使用selenium web驱动程序上载文件。浏览文件工作正常。但是当我把一个文件上传到那个本地主机站点时,我感到困惑。有一个上传按钮。代码如下所示Java 如何使用selinium web驱动程序上载文件?,java,firefox,selenium,selenium-webdriver,Java,Firefox,Selenium,Selenium Webdriver,我正在尝试使用selenium web驱动程序上载文件。浏览文件工作正常。但是当我把一个文件上传到那个本地主机站点时,我感到困惑。有一个上传按钮。代码如下所示 <input type="submit" value="Upload" > </input> 这两个都不适合我。给出的错误消息未提及该名称或id。我应该如何更改上述代码 使用简单的发送键上传 driver.findElement(By.cssSelector("[value='file']")).sendK
<input type="submit" value="Upload" > </input>
这两个都不适合我。给出的错误消息未提及该名称或id。我应该如何更改上述代码
使用简单的发送键上传
driver.findElement(By.cssSelector("[value='file']")).sendKeys("C:\Users\IEUser\Desktop\Saifurs\Test.html");
driver.findElementBy.cssSelector("[value='Upload']")).click();
使用此代码上载并单击上载按钮:(假设上载文件的元素是一个类型为“file”的“输入标签”) 备选方案:- 以下是上述方法的替代方法
请只导入与“java.awt”包相关的类,因为“Robot、KeyEvent、StringSelection和Toolkit”只有在那时才起作用
//Clicking on the Browse button
driver.findElement(By.xpath("//input[@type='file']")).click();
//Setting the Clipboard contents
StringSelection path = new StringSelection("C:\\Users\\user\\Desktop\\Ontology.owl");//the code needed to be pasted in the second dialog box
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(path, null);
Thread.sleep(4000);//Sleep time to detect the window dialog box
//Pasting the path in the File name field
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//To click the Open button so as to upload file
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
//Click on Upload button
driver.findElement(By.xpath("//input[@value='Upload']")).click();
试试这个
driver.findElement(By.xpath("input field")).sendKeys("path of the file which u want to upload");
输入字段表示
inputbox
的xpath
未工作。甚至不打开文件。我应该给什么类型的点击?如何获取id或名称?我的代码显示文件已选中,然后单击“上载”按钮“不工作”。id/name错误。您提供的html中没有id。您必须根据提供的html片段使用xpath或css选择器。而且,文件上传不需要click()
simple send key应该上传文件我应该如何更改代码?您的代码甚至不浏览文件。如何获取xpath或css?您能解释一下吗?Selenium文件上传不需要文件浏览或打开额外窗口或其他任何东西。请尝试我编辑的代码,并提供我添加的绝对文件路径图像。是的,我试过了。但是没有上传。我应该更改什么?现在我认为文件正在上载。但最后显示错误404:未找到消息。和浏览器链接,显示“会话/上载”。有什么问题吗?这一定是和网站的功能有关。手动完成后,上传是否正确?是的。这很好。我只是浏览并点击上传按钮而已。你怎么知道上传成功了?它是否显示任何确认信息或警告框或其他东西?抱歉,实际上文件并没有上传。我按下“后退”按钮并尝试手动单击“上载”按钮。那个时候也给出了同样的错误。但不需要从selenium加载站点并手动加载,我就可以上传。到底怎么了?
//Clicking on the Browse button
driver.findElement(By.xpath("//input[@type='file']")).click();
//Setting the Clipboard contents
StringSelection path = new StringSelection("C:\\Users\\user\\Desktop\\Ontology.owl");//the code needed to be pasted in the second dialog box
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(path, null);
Thread.sleep(4000);//Sleep time to detect the window dialog box
//Pasting the path in the File name field
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//To click the Open button so as to upload file
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
//Click on Upload button
driver.findElement(By.xpath("//input[@value='Upload']")).click();
driver.findElement(By.xpath("input field")).sendKeys("path of the file which u want to upload");