How to open multiple windows using selenium webdriver? - testing

I have to check switching behavior of sites but when i test it on site http://www.baidu.com , which in usual browsers, opens options in a new tab, opens the clicked option in the same tab while using selenium. How to make it execute as it executes normally in browsers?

Following code will help you to open link in new window :
driver.get("http://www.google.com"); //Replace your URL
WebElement link = driver.findElement(By.xpath("Your Element Xpath"]"));
Actions act = new Actions(driver);
act.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// It will close new opened window
driver.close();
You can use id or other locator for element as per your flexibility.
We are just pressing keys Shift + Up by selenium code.

Related

How to click on save button in chrome print privew page using selenium Java

I am currently looking for a solution to click on Sava button in chrome print preview window with selenium Java.
Is there any way we can handel chrome print preview page?
I have tried with the Robot class, but it seems not reliable/stable for my application.
Could you please someone help me to achieve this with selenium Java.
I fail to see any "Save" button on print preview page for Chrome browser
Here is how you can click "Cancel" button, I believe you will be able to amend the code to match your requirements:
First of all make sure to wait for the preview page to be available using Explicit Wait
Change the context to the print preview window via WebDriver.switchTo() function
All the elements at the print preview page are hidden in ShadowDom therefore you will need to:
Locate the element which is the first parent of the element you're looking for
Get its ShadowRoot property and cast the result to a WebElement
Use the WebElement.findElement() function to locate the next parent
repeat above steps until you reach the desired button
Example code just in case:
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
driver.switchTo().window(driver.getWindowHandles().stream().skip(1).findFirst().get());
WebElement printPreviewApp = driver.findElement(By.tagName("print-preview-app"));
WebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
WebElement printPreviewSidebar = printPreviewAppConten.findElement(By.tagName("print-preview-sidebar"));
WebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
WebElement printPreviewHeader = printPreviewSidebarContent.findElement(By.tagName("print-preview-header"));
WebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader, driver);
printPreviewHeaderContent.findElements(By.tagName("paper-button")).get(1).click();
where expandShadowRoot function looks like:
private WebElement expandShadowRoot(WebElement parent, WebDriver driver) {
return (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", parent);
}
Save button will not work because page is not part of webpage. selenium only support web based application.
But you can use SIKULI to handle above scenario.

How to click on a check box in a new window through selenium webdriver

I am trying to make this check box click but not I'm not able to click on check box.
Please send me the xpath or css
I tried:
xpath
//div//*[#id='thCheckBox']
.//*[#id='searchOrderModel']/div/div/div[3]/div/button[2]
There is a label next to the checkbox. You can try to click on the label then it will click the checkbox using below x-path.
//label[#for='thCheckbox']
As per the HTML you have provided and your question as the Check Box is on a new window to click() on the Check Box you have to induce WebDriverWait for the Check Box to be clickable and you can use the following Locator Strategy :
cssSelector :
"table.table.table-hover.dataTable#orderNoDropdown label[for='thCheckBox']"
xpath :
"//table[#class='table table-hover dataTable' and #id='orderNoDropdown']//label[#for='thCheckBox']"
As you have mentioned it is new window, then you will have to switch to new windows in order to interact with the web element. after reaching to new window you can use this code :
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Now you have focused on new Windows , and you can interact with check box now :
WebElement checkBox = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("thCheckBox")));
checkBox.click();
Then again you have to switch back to page from where you went to this Page, if you want that.
driver.close();
driver.switchTo.windows(tabs.get(0));

Selenium Web Driver

We are working on IE Automation using Selenium Web driver in C#.Net.
We are getting an exception in handling model popup window. We supposed to do below the action.
When we click on Link button it will open a popup window then we need switch to popup window selecting check box options and click on Submit button.
When clicking on Link button we are able to open the popup window. But here we are facing an issue like the child popup window is not loading with data and getting HTTP 500 Internal server Error.
I don't understand sometimes it was working properly with the same code but not all the times I am getting above issue when I am trying to perform above actions on child window.
is this any IE settings issue or my code issue even i ignored protected mode settings in IE settings.
I am trying with below code :
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[#id='ByNewNotes']")));
(or)
string jsWindowString = "NewWindow('pop_Type.jsp?Type=External&IuserId=NUVJK50'," + sessionId + ",'400','500');return false";
((IJavaScriptExecutor)driver).ExecuteScript(jsWindowString);
Could you please help on this issue.
Thanks in Advance.
Instead of using ExpectedConditions.ElementEx‌​ists use ExpectedConditions.elementToBeClickable or presenceOfElementLocated
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""//*[‌​#id='ByNewNotes']")));
element.click();
Either Try to use FluentWait. Create a by function of your element which you want to wait and pass it in below method
WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}
Hope it will help you :)
Have you tried
Thread.Sleep(2000);
We had the same issues and solved it with this simple way.

Can we open a link in new tab (not a new window)in selenium and give commands for carrying out tasks

Can we open a link in new tab (not a new window) and give commands for carrying out tasks
And is it possible to force selenium to open it in new tab.(just like we do Right click - open in new tab)
and then how to handle the tabs
Selenium does not provide a native way to open a new tab. How ever you can use workarounds. Every browser has some short cut for opening new tabs.You can use sendskeys method to do that.
Try the following code:
WebDriver DRIVER=new FirefoxDriver();
DRIVER.get("http://google.com");
WebElement El1=DRIVER.findElement(By.xpath("//body"));
El1.sendKeys(Keys.CONTROL,"t");
DRIVER.get("http://google.com");
Selenium Supports Mouse Hover actions and Right Click functionality, where user can right click on link and choose to open in new Tab.
WebDriver driver = new FirefoxDriver();
driver.get(URL);
Actions act = new Actions(driver);
WebElement linkpath = driver.Findelement(by.xpath(path of the link));
act.contextclick(linkpath).perform(); // right click
act.sendkeys("T").perform(); // click on new tab

Opening a new tab in the same window session of the browser through Selenium WebDriver?

How to open a new tab in the same window session of the browser through Selenium WebDriver command?
Opening a new tab in the same browser window is possible, see solutions for Firefox:
How to open a new tab using Selenium WebDriver with Java?
Controlling firefox tabs in selenium
The problem is - once you've opened a tab, there is no built-in easy way to switch between tabs. Selenium simply doesn't provide an API for that.
Instead of a tab, open a new browser window.
Yes you can do that , See below my sample code for that :
//OPEN SPECIFIC URL IN BROWSER
driver.get("http://www.toolsqa.com/automation-practice-form/");
//MAXIMIZE BROWSER WINDWO
driver.manage().window().maximize();
//OPEN LINKED URL IN NEW TAB IN SAME BROWSER
String link1 = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.linkText("Partial Link Test")).sendKeys(link1);
Above code will open link1 in new tab. you can run above code to see effect. Above is public link includes testing form.
But as #alecxe told that there is no way to switch between tabs. So better you open new browser window.
Using java script we can easily open new tab in the same window.
public String openNewTab(){
String parentHandle = driverObj.getWindowHandle();
((JavascriptExecutor)driverObj).executeScript("window.open()");
String currentHandle ="";
// below driver is your webdriver object
Set<String> win = driver.getWindowHandles();
Iterator<String> it = win.iterator();
if(win.size() > 1){
while(it.hasNext()){
String handle = it.next();
if (!handle.equalsIgnoreCase(parentHandle)){
driver.switchTo().window(handle);
currentHandle = handle;
}
}
}
else{
System.out.println("Unable to switch");
}
return currentHandle;
}
I am afraid, but there is a way to switch between tab's .
We have successful answers for this problem.
Please find the below link.
switch tabs using Selenium WebDriver with Java
Selenium can switch between tabs.
Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
element = driver.find_element_by_css_selector("html") # Gets the full page element, any element works for this
key_code = Keys.CONTROL # Use Keys.COMMAND for Mac
driver.execute_script("window.open();") # Executes Javascript to open a new tab
driver.switch_to.window(1) # Switches to the new tab (at index 1, first tab is index 0)
driver.switch_to.window(0) # Switches to the first tab