如何使用“上传图片按钮"上传文件/照片在Selenium Webdriver中 [英] How to Upload File/Photo using "Upload Image Button" in Selenium Webdriver
本文介绍了如何使用“上传图片按钮"上传文件/照片在Selenium Webdriver中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
WebElement UploadImg = driver.findElement(By.xpath("//*[@id='file_upload_button']"));
UploadImg.click();
WebElement frame =driver.switchTo().activeElement();
frame.sendKeys("d:\5.jpg");
此代码只是打开系统窗口,但未选择任何照片/文件
This code just open system window but it doesn't select any Photo/File
推荐答案
运行此代码:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1])",` driver.findElement(By.xpath("//input[@type='file']")), "0");
js.executeScript("arguments[0].setAttribute('class', arguments[1])", driver.findElement(By.xpath("//input[@type='file']/../../div[2]")), "a");
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("Your Path to the file your system");
说明:每个浏览"按钮在DOM中均处于隐藏状态,并且在DOM中都有一个<input>
标记.通过使用下面的代码行,我们只需更改包围<input>
标签的标签的类和样式属性,即可使其可见并可以对其执行sendKeys()
命令.之后,对要上传的图像/文件的绝对路径执行sendKeys
时.
Explanation: Every Browse button has a <input>
tag in DOM in hidden state . By using the below lines of code, we just change the class and style attributes of the tags enclosing that <input>
tag so that it becomes visible and a sendKeys()
command can be performed on it. After that when you do a sendKeys
with the absolute path of the image/file you want to upload.
这篇关于如何使用“上传图片按钮"上传文件/照片在Selenium Webdriver中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文