open a link in new tab in selenium - selenium

I am trying to open a link in new tab.
Trial1
Actions action = new Actions(Driver).KeyDown(Keys.Control).KeyDown(Keys.Shift).Click(FindElement(xxx).KeyUp(Keys.Control).KeyUp(Keys.Shift);
action.Build().Perform();
This code opens the link in new tab at the same time opens another blank window
Trial2 - right click the link and choose the first option "open link new tab"
action.ContextClick(FindElement(xxx).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();
This opens the link in new window instead of new tab

So what you need to do is first open a new tab using JS executor:
((JavascriptExecutor)driver).executeScript("window.open()");
or
((JavascriptExecutor)driver).executeScript("window.open('the new link here', '_blank')");
If you did not use the secon method continue with the following: switch to the new window and navigate to the link
driver.switchTo().window(here the window handle);
driver.get("here is the link");

Related

it opens new tab when we click on Print button

i am login in to application ,after that i have to fill data in some fields ,after that i have to click on the print toolbar button ,when i click print button it is opening new tab with unique descriptor (i.e it changes every time when we click print).so i need to navigate to that tab.
i have tried some code:
here is my code
enter code here`await t .click(Selector('# ddlpreBankAcc - pi');
await t. click(Selector('# dllprepredistributionType');
await t. typeText(Selector('#dateofevent').nth(1), d).setTestSpeed(0.6)
await t. click(Selector('#btnPrePrint');`
After this line it is opening new tab in chrome .
how to write code for that .
but in that new tab descriptor is unique descriptor.

Selenium Webdriver - Accessing 2nd Child window(Popup)

I am automating an application in IE where if user clicks a link on the main window , a child window popups. User further clicks another link from the child window where a 2nd child window popsup. Please find the screenshot of the same application screenshot
Problem is that the 3rd popup window is behind the 2nd popup window. with the following code i am able to get the title for the 3rd popup window, but cannot able to work (Like click on any link etc) over there.
Please find below the code which i have used to navigate to 3rd window from the 2nd.
`String Mw1 = driver.getWindowHandle();
//User clicks a radio button on 2nd window
driver.findElement(By.id("CallType-0")).click();
//User click a submit button and after this the 3rd window popsup
driver.findElement(By.id("cmdLogCall")).click();
Set<String> r1=driver.getWindowHandles();
Iterator<String> i2 =r1.iterator();
while (i2.hasNext())
{
String childwindow2 = i2.next();
if(!Mw1.equalsIgnoreCase(childwindow2))
{
driver.switchTo().window(childwindow2);
String z = driver.getTitle();
System.out.println(z);
driver.findElement(By.id("overridelink")).click();
}
}`
Kindly let me know how can i access the 3rd window.Thanks
Use the following code to switch onto the required window
ArrayList<String> allWindows = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(allWindows.get(2));
System.out.println(driver.getTitle());
driver.switchTo().defaultContent();

How to close pop up window, when there is no XPath for close button?

When I clicked on a some link, the popup window is opened on new browser.
I want to close that pop up window, but I am unable to find out the locator.
How to close window, when it is opened as popup in Selenium?
Use this this,it worked for me
// Before new window
String currentWindow=driver.getWindowHandle();
//After window new open
Set<String> handles=driver.getWindowHandles();
//to make set having only one single/current window value
handles.remove(currentWindow);
String[] handlesArray = handles.toArray(new String[handles.size()]);
driver.switchTo().window(handlesArray[0]).close();

Close download bar

I am using Java and Selenium to write a test. Somewhere in my test, I download a file but then need to click on a button under the download bar which appears at the bottom of the chrome browser page. I search a lot but the only solution was here which is not my case as I don't have a scroll.
I also use:
action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);
ArrayList<String> tabs2 = new ArrayList<String> (driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs2.get(1));
Thread.sleep(500);
driverChrome.close();
driverChrome.switchTo().window(tabs2.get(0));
Thread.sleep(500);
but it doesn't open the download page.
Anyway that I can close the download bar?
This method did not work for me either, but I developed a workaround. I do any download test in a new window, then close the download window, the original window does not have the download bar. It must be a new window, if you do a new tab it will transfer over, to get this I use JavaScript. Switch to the new window, run download test and then switch to the original window when done.
string javascript = $"$(window.open('', '_blank', 'location=yes'))";
((IJavaScriptExecutor)Driver).ExecuteScript(javascript); //create new window
Driver.SwitchTo().Window(Driver.WindowHandles.Last())); //switch to new window
//do download test here
Driver.Close(); //close created window
Driver.SwitchTo().Window(Driver.WindowHandles.First()); //back to original window with no download bar

How to open silverlight childwindow as separate browser

when click on any button it should show the silverlight childwindow as a popup but insteadof modal popup i want to open child window as separate window/browser with the url
Please try this
HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
HtmlPage.PopupWindow(new Uri(http://localhost:51327/MyprintPage.aspx), "_newWindow", options);