When I click on link under menu which is inside the iframe, it opens the new browser window - selenium

In below code of selenium script to access iframe elements.
I open a menu in first webElement and and then in second webElement I click on link watches under menu.
It redirects the page properly but it opens same page in new browser window also.
i.e. 2 browser are opened. How to prevent new browser to be opened.
driver.switchTo().frame(driver.findElement(By.id("watches_iframe")));
Thread.sleep(3000);
WebElement menu = driver.findElement(By.xpath("//div[#id='pm-
nav']//div[#class='pm-menu-button']"));
menu.click();
// when I click on below link it redirects the page properly but opens same
// page in new window also
WebElement store = driver.findElement(By.xpath("//div[#id='pm-menu-
master']//li[#class='item-store']//a//span[1]"));
store.click();

Related

How to switch focus from WinAppDriver to IWebDriver?

I am working on Desktop application using WinAppDriver and Selenium C#.
There are couple of links on the application.
If we click on the link, it will redirect to default browser.
How I can switch focus from WinAppDriver to IWebDriver ?
How to verify the link, whether the link opened in default browser or not?
Please help on this. Thank you.
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
// Close the new window, if that window no more required
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

Selenium IE Driver opens link on a new window and not on new tab

I am using IE11 for my selenium tests. I Am clicking on a link which when done manually opens up the link on a new tab.
When I am trying to do the same thing with Selenium, it does not open the link on a new tab rather, it opens up on a new window.
Is there a way to force IE to click and open the link on a new tab?
I am able to achieve the same with Firefox and Chrome, but with IE, as soon as the link is clicked, it opens on a new window rather than a new tab.
if (BROWSER_TYPE.equals("IE")) {
Utility.log(this.getClass().getName(),Level.INFO,"Browser type is IE downloading firefox driver is required.");
WebDriverManager webDriverManagerObject = WebDriverManager.iedriver();
webDriverManagerObject.targetPath(DRIVER_DOWNLOAD_PATH);
if(!DRIVER_VERSION.equals("latest")) {
WebDriverManagerObject = webDriverManagerObject.version(DRIVER_VERSION);
}
Utility.log(this.getClass().getName(),Level.DEBUG,"Launching IE Browser");
webDriverManagerObject.setup();
driver = new InternetExplorerDriver();
}
Expected Result: Clicking on the Link should open the page on a new tab on the same window in IE
Actual result: Clicking on the Link opens the page on a new window rather than a new tab.

switch between tabs in chrome using arquillian graphene

I need to switch tabs using Graphene and continue the test without failing.
I am testing an application that creates a project and then opens that project from a link on a page.
My issue is that it opens the project in a new tab and to continue the test I have to switch to the new tab.
I am using the selenium switch windowHandler but getting an error from the Graphene. I will not know the new URL until the project has been created.
//Switch to tab opened from previous test
public void switchTab(int tabNumber) throws Exception{
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
Thread.sleep(3000);
}
}
Error: The page object 'CreateChangeSubProjStruct' that you are navigating to using either Graphene.goTo() or #InitialPage is not annotated with #Location
I can see the new page opens and is on top but the test is running on the original tab.

How to get title of page in Chrome browser when an alert in opened on the same page using selenium webdriver

In CHROME window, There is an alert opened. But without closing the alert first, is there any way to get window title of the page. Because When i try
driver.getTitle();
without closing the alert
Exception occurs : unexpected alert open.
Reason i want to very file page title is because there are multiple windows opened, & i wish to close alert only on particular page. I used the below code for switching to that window
for (String winhandle :driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
if (driver.getTitle().equals("some_title"))
{
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert(); alert.close();
}
}
NOTE : above mentioned same code works fine with FIREFOX. Issue occurs only with the chrome browser

Selenium throwing exception when a modal dialog is shown at start of ie instance

I have to automate a scenario where a modal dialog opens immediately after ie is open i.e.
from selenium import webdriver
driver=webdriver.Ie()
Once i do this, IE instance opens and an IE modal dialog opens on top of ie.
Now selenium closes this modal dialog immediately and throws an exception:
Unexpected alert present, Modal Dialog present.
But i have to handle this modal dialog.
Is there any way to handle this dialog and make sure selenium does not throw exception and quit?
Had the same problem, selenium dismissing the alert (as if Cancel was clicked), where you want to confirm (as if OK was clicked).
For me it worked to initialize the driver by initializing webdriver with the UnexpectedAlertBehavior option, as per below
private static InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions{ UnexpectedAlertBehavior = InternetExplorerUnexpectedAlertBehavior.Accept };
public static InternetExplorerDriver Driver = new InternetExplorerDriver(SeleniumLocator.GetSeleniumDriverPath(), internetExplorerOptions);