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.
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);
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);
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.
Related
New to automation and could use some help here.
I am using Selenium Webdriver and Java on this website - Webdriver University and so far this code has been throwing No Such Element exception at "element.click()" step (i.e., doesn't find the element on page):
driver.manage().window().maximize();
driver.get("http://webdriveruniversity.com");
Thread.sleep(3000);
// Follow the link to another page
WebElement link = driver.findElementByXPath("(//div[#class=\"section-title\"])[6]");
link.click();
Thread.sleep(3000);
// Click on the element
WebElement element = driver.findElementByXPath("(//button[#class='accordion'])[1]");
element.click();
However, when I go to the linked page directly, it finds the element just fine
driver.manage().window().maximize();
driver.get("http://webdriveruniversity.com/Accordion/index.html");
// Click on the element
WebElement element = driver.findElementByXPath("(//button[#class='accordion'])[1]");
element.click();
I've used wait for element visibility and Thread sleeps, same results.
Any idea what could be the issue here?
Did you notice that when you click the link, page is opened in new tab? That is your issue.
You need to switch to new tab.
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //here you are switch to second tab
Hope the below code will solve your issues.
Used the getWindowHandles() to capture handle of newly opened tab and switch to the
tab
// Follow the link to another page
WebElement link = driver.findElement(By.xpath("(//div[#class=\"section-title\"][6]"));
link.click();
Set<String> allWindow = driver.getWindowHandles();
Iterator<String> itr = allWindow.iterator();
while (itr.hasNext()) {
String wind = itr.next().toString();
driver.switchTo().window(wind);
}
Thread.sleep(3000);
// Click on the element
WebElement element = driver.findElement(By.xpath("(//button[#class='accordion'][1]"));
element.click();
Thread.sleep(3000);
driver.close();
}
I have a problem to click the element that is within an iframe. Before this step, after clicking one button, a new window is opening and then I am switching to a new window and then I am trying to click the element within an iframe, however without any results. Here you can see the code:
#Test
public void test01() throws InterruptedException {
LoginPageMethods loginPage = new LoginPageMethods();
HomePageMethods homePage = new HomePageMethods();
ShoppingCartMethods shoppingCart = new ShoppingCartMethods();
loginPage.loginToWebsite(Recordset.userName, Recordset.webPassword);
String parentWindow = driver.getWindowHandle();
System.out.println("The current window" + parentWindow);
homePage.goToShoppingCartPage();
//Return a set of window handle
ArrayList<String> windows = new ArrayList<>(driver.getWindowHandles());
System.out.println(windows);
driver.switchTo().window(windows.get(1));
System.out.println(windows.get(1));
//loginPage.loginToWebsite(Recordset.userName, Recordset.webPassword);
WebElement iframe = driver.findElement(By.xpath("//iframe[#id='contentAreaFrame']"));
driver.switchTo().frame(iframe);
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
int iframecount = iframes.size();
System.out.println("Number of iframes: " + iframecount);
shoppingCart.goToSetValuesOptions();
shoppingCart.setCompanyCode();
(I want to click the Set Values Link)
(inspected the element and this is visible - in Chrome)
(ultimately I need to execute that in Internet Explorer and checked in here via the MRI tool and it didn't recognize the xpath, but in CHrome it does).
Here is what I get in the console:
The current windowCDwindow-84EBD3BA474C418428AACFD82588EAF8
[CDwindow-84EBD3BA474C418428AACFD82588EAF8,
CDwindow-06F8C7313F9C0CF38B35BEA929AC2C0B]
CDwindow-06F8C7313F9C0CF38B35BEA929AC2C0B Number of iframes: 3
org.openqa.selenium.NoSuchElementException: Timed out after 30
seconds. Unable to locate the element
I'm trying to click on web element and enter text inside it.
Steps:
Launch "https://www.phptravels.net/"
Click on tours tab.
Perform send keys operation on search field.
1.I tried using click on search box and entering text via send keys but unable to do so, After that I performed click action and send keys using javaScript but this is also not working.
I have written different xpath for the same but no positive results.
//code is as below
public class HandlingDropDown2 {
static WebElement element;
static WebDriver driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "Driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.phptravels.net/");
element = driver.findElement(By.xpath("//span[contains(text(),'Tours ')]"));
element.click();
Thread.sleep(2000);
element = driver.findElement(By.xpath("//button[contains(text(),'Got it!')]"));
element.click();
Thread.sleep(2000);
element = driver.findElement(By.xpath("//div[#id='s2id_autogen5']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
System.out.println("clicked on autogen box");
//element.click();
Thread.sleep(2000);
element = driver.findElement(By.xpath("//div[#class='select2-drop select2-display-none select2-with-searchbox select2-drop-active']"));
JavascriptExecutor executor2 = (JavascriptExecutor)driver;
executor2.executeScript("arguments[0].'value='Test';",element);
//element.sendKeys("test");
}
}
Expected Result: User must be able to enter some text via automation.
Actual Result: Unable to perform click and sendkeys using JavaScript and simple selenium methods.
Remember the functionality of sendKeys
Fir of all, your xPath is div element and you are trying to do sendKeys in div element which is wrong. If you observed there is span element named 'Search by Listing or City Name'. If you click there then your input element gets visible where you can click() and sendKeys("")
Try,
// click on below span element to get input visibled,
element = driver.findElement(By.xpath("//span[text()='Search by Listing or City Name']"));
element.click();
Then your input element is now available where you can click and sendkeys
element = driver.findElement(By.xpath("//div[#id='select2-drop']//input[#class='select2-input'][last()]"));
element.click();
element.sendKeys("test");
url = http://www.yopmail.com/en/
public class ReadEmailForEmailChange {
WebDriver drive = new FirefoxDriver();
drive.get("http://www.yopmail.com/en/");
// I am able to get the size of frms
int size = drive.findElements(By.tagName("iframe")).size();
// this is the user name send keys.
WebElement emai = (WebElement) drive.findElement(By.xpath("//input[#id='login']"));
emai.sendKeys("testdenmark");
// this is button to click. it is working as well
WebElement emaenter = (WebElement)drive.findElement(By.xpath("//input[#title='Check inbox #yopmail.com']"));
emaenter.click();
// this is 1st frame. i am able to switch to this frame with no prob
drive.switchTo().frame(drive.findElement(By.id("ifinbox")));
// this is to find the email based on subject text. it is working fine
WebElement emailopen = (WebElement) drive.findElement(By.xpath("//span[#class='lms' and text()='email verification']"));
emailopen.click();
System.out.println("got to the emial.");
// here comes problem. i am 100% that Id is correct or xpath is correct
but i am not able to switch to this frame. I am getting element not found except
//WebElement frame = drive.findElement(By.xpath("//tr//td//iframe[#id='ifmail']"));
//drive.switchTo().frame(drive.findElement(By.id("ifmail")));
//drive.switchTo().frame(drive.findElement(By.xpath("//iframe[#id='ifmail']")));
this will happen after i switch to the frame
drive.findElement(By.xpath("//a[text()='Verify email']")).click();
The iframe is
<iframe class="whc" frameborder="0" scrolling="auto" id="ifmail" name="ifmail" src=""></iframe>
Sounds like you need to wait for the iframe to appear first. Try a webdriver wait before attempting to switch to the IFrame.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ifmail")));
i am trying to get src attributes of all nested iframes but i am getting stale reference exception.Here is the code
public class findAllSources {
ArrayList<String> sources = new ArrayList<String>();
#Test
public void iframeTest() {
System.setProperty("webdriver.chrome.driver", "path to chrome driver");
ChromeDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.engadget.com/");
List<WebElement> allFrames = driver.findElements(By.xpath("//iframe[not(contains(#style,'display:none'))]"));
for (WebElement frame : allFrames) {
if(frame.isDisplayed()){
System.out.println("We clicked frame "+frame);
System.out.println(" with a source "+frame.getAttribute("src"));
sources.add(frame.getAttribute("src"));
driver.switchTo().frame(frame);
findDeeperFrames(driver);
driver.switchTo().defaultContent();
}
}
}
public void findDeeperFrames(ChromeDriver driver){
List<WebElement> deeperFrames = driver.findElements(By.xpath("//iframe[not(contains(#style,'display:none'))]"));
for (WebElement nframe: deeperFrames) {
if (nframe.isDisplayed()){
if (!nframe.getAttribute("src").isEmpty()){
sources.add(nframe.getAttribute("src"));
System.out.println("Nested source "+ nframe.getAttribute("src"));}
driver.switchTo().frame(nframe);
findDeeperFrames(driver);
//getting out of the frame
driver.switchTo().defaultContent();
}
}
}
}
Is there any other approach by which i can achieve my goal?
I encountered the same StaleElementReferenceException when I tried to interact with an iframe (stored as a WebElement variable), that was actually still visible on the page, so I had no idea what caused the issue. The reason was that first, I stored the iframe as a variable, after that, I switched to that frame using driver.switchTo().frame(thatFrame); (to interact with elements inside the frame), but I never switched back to the original, main frame with driver.switchTo().defaultContent();, before I wanted to interact with the frame itself again. And "from inside" the frame, the actual frame DOM element is a stale object (not attached to the inner frame's DOM).
Maybe not an exact answer to your problem, just a note for those who search for this exception and frames.
UPDATED:
Your code is returning StaleExcceptionError, because after using driver.switchTo().defaultContent(); your code gets into next Iteration of for LOOP which is still using allFrames webElement list which was stored initially before switching to new frame and Those elements are not attached to DOM anymore. So in order to avoid that you have to Identify that frame s list once again as shown below:
List<WebElement> deeperFrames = driver.findElements(By.xpath("//iframe[not(contains(#style,'display:none'))]"));
for (int j=1; j<=deeperFrames.size(); j++) {
List<WebElement> myFrames = driver.findElements(By.xpath("//iframe"));
WebElement nframe = myFrames.get(j-1);
if (nframe.isDisplayed())
{
if (!nframe.getAttribute("src").isEmpty()){
sources.add(nframe.getAttribute("src"));
System.out.println("Nested source "+ nframe.getAttribute("src"));}
driver.switchTo().frame(nframe);
findDeeperFrames(driver);
//getting out of the frame
driver.switchTo().defaultContent();
}
}