we need to switch to the second window select title is not working in this case how can we do that pleaseenter image description here
问题:
回答1:
1) Using sendkeys we can send file path name:-
It’s the most basic technique to perform the upload of a file.Get the file upload element either by using the Id or Name. And call the Webdriver’s sendKeys() method to set the value of the file to upload.
Remember following two things when uploading files in WebDriver
1)There is no need to simulate the clicking of the "Browse" button. WebDriver automatically enters the file path onto the file-selection text box of the <input type="file">
element.
2)When setting the file path in your Java IDE, use the proper escape character for the back-slash.
Try this:-
WebDriver driver = new FirefoxDriver();
// Put an Implicit wait,
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://toolsqa.com/automation-practice-form");
WebElement element = driver.findElement(By.id("photo"));
element.sendKeys("/home/savera9/Desktop/test.jpg");
There is another techniques also for uploading file please check this
2) Using Robot Class:-
driver.findElement(By.xpath("Path of that element")).click();
StringSelection strSel = new StringSelection("upload file path");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSel, 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);
Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
3) Using AutoIT:-
AutoIT helps to upload files by transferring the control from Selenium web driver to AutoIT. We need to explicitly call the AutoIT script from our program. After clicking on upload button, the focus will be moved to AutoIT and it will execute the statements which will be used to upload files.
4) Using Sikuli:-
Sikuli is an open source Graphical User Interface automation tool. Sikuli will be used to automate anything that you can view on the screen. It uses image recognition to speak with the GUI elements. When there is no easy access to a GUI’s source code this is one of the best ways to get the appropriate response.
There is another ways also to upload file go through this link https://www.evoketechnologies.com/blog/selenium-automation-uploading-multiple-files-via-web-browsers-file-dialog/