I have IE 11. I am using the ie driver 3.14 32bit and writing my test in java. My os is win 10.
When I click on an element it works fine:
linkElement.click();
When I control click on the element it acts as if it is just a click:
action.keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
My goal is to open the link in a new tab - that is why I am control clicking. How do you open a link in a new tab in ie?
EDIT
Here is a simple test:
InternetExplorerOptions options = new InternetExplorerOptions();
options.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
URL driverUrl = TestTest.class.getClassLoader().getResource("IEDriverServer.exe");
String driverPath = driverUrl.getPath();
System.setProperty("webdriver.ie.driver", driverPath.toString());
InternetExplorerDriver driver = new InternetExplorerDriver(options);
driver.manage().window().maximize();
driver.get("http://www.cnn.com");
WebElement linkElement = driver.findElement(By.xpath("//*[#id=\"nav\"]/div[2]/div[2]/a[2]")); // get World link
new Actions(driver).keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
With my simplified test it opens in a new window - I have the setting in IE to open in a new tab. How can I make it open in a new tab?
Related
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
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.
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
I am using Selenium for chrome browser automation(OS-windows only). I want to open a webpage and fill a login form , submit.But i dont want to show this login page(chrome browser) to the user.That is user will not see the chrome window which shows filling the form and clicking submit. The user must be able to see chrome only after clicking submit(which i click in my code). So i need to hide the chrome(only this window,but if user has kept some other chrome window open they should not be made visible false) for a while and then after login i want to make chrome window visible.
Below is a part of code in which chrome browser opens with login page and form filled automatically and submit is clicked and new page navigated(after login)
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim driver1 As ChromeDriver = Nothing
Dim driver As EventFiringWebDriver
Dim chromeOptions As New OpenQA.Selenium.Chrome.ChromeOptions()
chromeOptions.AddExcludedArgument("ignore-certifcate-errors")
chromeOptions.AddArgument("test-type")
service.HideCommandPromptWindow = True
driver1 = New ChromeDriver(service, chromeOptions)
driver = New EventFiringWebDriver(driver1)
AddHandler driver.Navigated, AddressOf OnNavigated
driver.Navigate().GoToUrl("https://example.com/login.htm")
Dim myLink1 As IWebElement = driver.FindElement(By.Name("userName"))
myLink1.SendKeys("myusername")
Dim myLink2 As IWebElement = driver.FindElement(By.Name("password"))
myLink2.SendKeys("mypassword")
driver.FindElement(By.CssSelector("input[type='submit']")).Click()
So now can I make the chrome window to not visible till login and then make it visisble in selenium or is there any other trick where I can achieve the same.
We are doing automation testing and came around with a situation where i need to download the file from the browser .
In Download when the download button is hit we are coming to the system pop for the download where we need to perform the enter operation .
Can some one help us how to perform the enter or keyboard operation currently we are using robot API but it is not working on grid system ,
Here is my code for robot can it be enhanced and used or do we have any alternate way to do it
******** Code *************
public void downloadReportFromMyExport(WebDriver driver, String downloadSufixId) throws AWTException,
InterruptedException
{
String downloadPrefixId = ConfigProperty.getConfig("downloadPrefixId").trim();
String[] suffix;
suffix = StringUtil.split(downloadSufixId, "TR_EXP_");
String suffixPart = suffix[0];
String completeId = downloadPrefixId.concat(suffixPart);
By id = By.id(completeId);
WebElement element = driver.findElement(id);
element.click();
Robot pressKey = new Robot();
pressKey.keyPress(KeyEvent.VK_ENTER);
pressKey.keyRelease(KeyEvent.VK_ENTER);
threadSleep("5");
pressKey.keyPress(KeyEvent.VK_ALT);
pressKey.keyPress(KeyEvent.VK_F4);
pressKey.keyRelease(KeyEvent.VK_F4);
pressKey.keyRelease(KeyEvent.VK_ALT);
logger.info("Downlaod Complete");
}
In firefox browser,
Solution-1
You can change the browser settings so that it saves all downloads to that location without asking.
Refer below link to know to change that setting in firefox.
https://support.mozilla.org/en-US/kb/startup-home-page-download-settings
Solution-2
By using firefox profile setting you can achieve this.
FirefoxProfile profile=new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir","C:\\Users\\Downloads\\"); profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver=new FirefoxDriver(dc);
yeah i've encountered the same issue
better change the browser settings to save in a particular path
for handling different browsers like,
in FF,
i've used
in firefox by default the control will be on "OPEN" option so..
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
for IE (to save alt+s,to open alt+O) here im saving the file
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
for chrome
by default,when ever you click dowload button it will save without showing any popups
and i've succeeded hope it helps you
-Ajay