Reputation: 21
In www.jobserve.com, when you click "Apply" (apply to any of the job postings), you will get "Job Application " pop up.
How can I tell Selenium to read elements from this pop up? Currently none of the elements in the pop up is recognised by Selenium.
Lets say I want to Upload my CV, then how do I do it?
I have tried Switch to a frame, but it appears the pop up is not a frame so it didn't work.
Upvotes: 1
Views: 432
Reputation: 21
Finally I made it. With Selenium Recorder Plugin I found out that I need to switch to frame 0 (see screenshot attached). So the answer is:
driver.switchTo().frame(0);
Upvotes: 1
Reputation: 1943
To Switch between iFrames we have to use the driver’s switchTo().frame
command. We can use the switchTo().frame()
in three ways:
switchTo.frame(int frameNumber): Pass the frame index and driver will switch to that frame.
switchTo.frame(string frameNameOrId): Pass the frame element Name or ID and driver will switch to that frame.
switchTo.frame(WebElement frameElement): Pass the frame web element and driver will switch to that frame.
WebDriver driver = new FirefoxDriver();
driver.get("https://www.jobserve.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(),'Job Search')]")));
element.click();
driver.findElement(By.xpath("/html/body/form/div[8]/div[1]/a/span")).click();
driver.findElement(By.xpath("//*[@id=\"searchtogglelink\"]")).click();
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"td_apply_btn\"]")));
element1.click();
Thread.sleep(2000);
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);
driver.switchTo().frame(0);
WebElement Upload_File = driver.findElement(By.xpath("//input[@id='filCV']"));
Upload_File.sendKeys("Path of file");
Using switchTo().frame()
you can switch to that frame and perform all actions on that opened frame.
There are two ways to find total number of iFrames in a web page. First by executing a JavaScript and second is by finding total number of web elements with a tag name of iFrame. Here is the code using both these methods:
//By executing a java script
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return
window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);
//By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
System.out.println("The total number of iframes are " +
iframeElements.size());
i)Switch to Frames by Index- Index of an iFrame is the position at which it occurs in the HTML page.On this page only one frame is present.to switch to 0th iframe we can simple write driver.switchTo().frame(0).
//Switch by Index
driver.switchTo().frame(0);
ii)Switch to Frames by Name- Name attribute has a value iframe1. We can switch to the iFrame using the name by using the command driver.switchTo().frame(“iframe1”).
//Switch by frame name
driver.switchTo().frame("iframe1");
iii)Switch to Frame by ID- iFrame tag we also have the ID attribute. We can use that also to switch to the frame. All we have to do is pass the id to the switchTo command like this driver.SwitchTo().frame(“IF1”).
//Switch by frame ID
driver.switchTo().frame("IF1");
iv)Switch to Frame by WebElement-
Now we can switch to an iFrame by simply passing the iFrame WebElement to the driver.switchTo().frame() command. First find the iFrame element using any of the locator strategies and then passing it to switchTo command.
//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("IF1"));
//now use the switch command
driver.switchTo().frame(iframeElement);
For more information go through this link.
Upvotes: 0
Reputation: 373
you can use any of following method to handle pop-up.
1) No need to switch to modal / frame. You can directly use findById, name or class to move to that modal element.
2) If you want to upload any file then just follow points :
a) Upload your file in tmp directory on server for temporary backup. like
String path = FILE_UPLOAD_COMMON_PATH + File.separatorChar + file.getName();
try(FileOutputStream fileOutputStream = new FileOutputStream(path)){
fileOutputStream.write(bs); // byte[] bs
}catch(Exception e) {
throw e;
}
b) Now get from tmp directory and upload file using driver like,
String path = FILE_UPLOAD_COMMON_PATH + File.separatorChar + file.getName();
driver.findElements(By.id("files")).get(0).sendKeys(path);
Upvotes: 0