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
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();
}
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.
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")));
Select se = new Select(driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select")));
se.selectByIndex(7);
driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select/option[8]")).click();
Above code doesn't work,please help
Error returned:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found
org.openqa.selenium.NoSuchWindowException: no such window
Means the browser is close when you are trying to interact with it. Remove driver.close() from your code and put it only after you have finished all you interactions with the browser.
Edit
If you need to return to parent window after closing child window use driver.switchTo() again
// get parent window ID
String parentHandle = driver.getWindowHandle();
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parentHandle))
{
driver.switchTo().window(handle);
}
}
//do something with the new window
// switch back to the old window
driver.close();
driver.switchTo().window(parentHandle);
windowIdbefore = driver.getWindowHandle();
System.out.println(windowIdbefore);
Set<String> windowid = driver.getWindowHandles();
for (String string : windowid) {
System.out.println(string);
driver.switchTo().window(string);
// enter code here
}
WebDriver driver=new FirefoxDriver();
Select s=new Select(driver.findElement(By.xpath("xpathExpression")));
s.selectByVisibleText("text");
s.selectByValue("value");
s.selectByIndex(1);
as i see here the dropdown box is present in div tag. i think with your code dropdown has been located but you are not able to select the value present in dropdown. Then follow below code
WebDriverWait wait = new WebDriverWait(d, 10);
Actions builder = new Actions(d);
WebElement selectvalue = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your drop down xpath value")));
builder.mouse.mouseMove(((Locatable)selectvalue).coordinates);
selectvalue.click();
WebElement option = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("locator value of dropdown value(your dropdown value)")));
builder.mouse.mouseMove(((Locatable)option).coordinates);
option.click();
System.out.println("dropdown value slected...");