I'm using Selenium2 (Webdriver).
Well, I have a Problem.
I have a A Browser.
When I click one button in A Browser, Another popup browser(B) is open.
And, The process what I have to do in B browser is done, B browser wasn't close.
But, this is not my problem. B browser is originally Designed this way.
my problem is ..
After B browser's process is finished, when I tried to find out C element in A browser, So B browser wans't closeed, that I can't find out C element. because Selenium tried to find out C element in B browser.
I just want...
When I do something in Browser B, Browser B has a Handle.
and the Process in Browser is done, the handle must moving in A Browser.
How can I? Please Help me.
Here is the sample code for switching between the Windows:
With windows handle we can switch between windows.
WebDriver driver=new FirefoxDriver();
//First navigating to Yahoo site
driver.get("http://www.Yahoo.com");
//Capturing the window handle
String strMainWindowHandle=driver.getWindowHandle();
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Now opening a new window
driver.findElement(By.xpath("//body")).sendKeys(Keys.CONTROL+"n");
Set<String> winHandles=driver.getWindowHandles();
for(String handle:winHandles)
driver.switchTo().window(handle);
//Navigating to Google site with new window i.e new window handle
driver.get("http://www.google.com");
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Switching back control to main Window which captured earlier
driver.switchTo().window(strMainWindowHandle);
System.out.println(driver.getTitle());
Once you come back to your main window handle you can perform Click or whatever u want.
Related
Browser chrome will be opened by selenium and executing some operations, but its seems to be not bring to front even it focused. How can i make this chrome window bring to front?
Here is what you need to do:
Before you switch to any other window opened store the parent window handle in a String: String parent_window = driver.getWindowHandle();
Switch to any other window & perform your actions.
Finally, switch back to the parent window through the parent window handle stored in a String: driver.switchTo().window(parent_window);
Perform the rest of your operations and call driver.quit();
Let me know if this Answers your question.
Set this
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
and then add this line
WebDriver driver=new ChromeDriver();
I'm trying to check whether the popup window I want to open is opened or not.
I have checked some question answers like
How would you check if a popup window exists using selenium webdriver?
But, nothings helped to solve the problem.
Here, first I open the login window by clicking the login button.
driver.findElement(By.xpath("//a[#id='login_btn']")).click(); // Click Login Button
I even tried getPageSource() but, it seems not working.
Any kind of help would be appreciated.
Thanks in advance. :)
If it's a native, browser alert (= a popup) you can do the following:
try{
driver.switchTo().alert();
// If it reaches here, it found a popup
} catch(NoALertPresentException e){}
What it looks like you're actually dealing with is an iframe which you can do the following after getting the attribute value of the "iframe" attribute:
driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope
String mwh=driver.getWindowHandle();
Now try to open the popup window by performing some action:
driver.findElement(By.xpath("")).click();
Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows
Iterator ite=s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mwh))
{
driver.switchTo().window(popupHandle);
/**/here you can perform operation in pop-up window**
//After finished your operation in pop-up just select the main window again
driver.switchTo().window(mwh);
}
}
I have one test case where after login, on some page when user tries to close the browser, it will show popup windows(alert) asking "you might lose the data, are you sure you want to continue?', with two options:
Leave the page
Stay on page
Clicking on specific option, the page will perform action.
'Stay on page' will not leave the page and Leave the page will close the browser.
Now when I try to close the browser, it doesn't ask me for Popup
webdriver.close()
closes the browser before.
How can I Accept/Reject popup and then based on action, it should close the browser?
If I am understanding the problem correctly, then you are trying to perform a click on X to close the browser window which generates additional pop up. If that's the case, you can try executing some JavaScript action to recreate the scenario
(( JavascriptExecutor ) webdriver).executeScript( "window.close()" );
instead of webdriver.close()
Note: Written in Java
More info:
With the syntax above you can only close the child tab not the entire browser only IF it is invoked with window.open()
I need to switch between the browser tabs, used the following code,
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
It was working properly sometimes only, but sometimes it is showing an exception.
Can anyone suggest me is there any other instructions for switching tabs within a single window by using java.
You have to use window handle function here. Here is a sample working code in java:
String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
Hope this helps!
Switching between browser window is different from switching b/w tabs.
In some browser windowhandler command may work but it wont work in all browser.
Here is the solution to navigate b/w tabs
for navigating left to right side:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();
For navigating right to left :
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
In my case, the following code is working fine-
String oldTab=driver.getWindowHandle();
driver.findElement(pageObj.getL_Popup_Window()).click();
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
WebElement ele = driver.findElement(pageObj.getI_input_name());
ele.click();
ele.sendKeys(name);
driver.findElement(pageObj.getI_submit()).click();
driver.switchTo().window(oldTab);
i didn`t find any useful info about my problem. sorry if i repeat.
for example i want to click at the main page of http://www.bbc.com/ in the bottom of site link "Mobile site". in casual i do smth like this, to click on my button:
driver.getMouse(driver.findElement(By.Id("blq-footer-mobile"))).click();
but now i need to simulate the activity of user.
1. i need to scroll the page to bottom
2. need to move the cursor on link
3. click it
i realy tried all what i found in the internet, but everything wrong.
WebDriver simulates user interactions with web applications using native browser APIs. So as long as you are using pure WebDriver API, you are simulating natural user. You don't need to explicitly scroll, WebDriver would do that for you. If it's not scrolling then it is a bug and please report it accordingly. As for your question, here is the code that works.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.bbc.com/");
WebElement element = driver.findElement(By.id("blq-footer-mobile"));
element.click();
The Mobile site link in the above website will just take u to UK website of BBC..
which means, a click on Mobile site link in http://www.bbc.com/ will actually lead you to http://www.bbc.co.uk/, where in the page remains same with just the URL changed..
if you really want to experiment on Mobile site link, use this URL : http://www.bbc.co.uk/
you can try the following code :
WebDriver driver = new FirefoxDriver();
driver.get("http://www.bbc.co.uk/");
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.id("blq-footer-mobile"))).click();
this will wait for elements visibility and click on it,and this will take you to actual mobile site of BBC..