I can't hide the address bar using selenium
I have read about selenium's capabilities with IE but I can't find the specific one
I hope to have an IE window without the address bar using selenium integrated with java
This is my code:
public class SeleniumIE {
static WebDriver visor = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "C:\\Users\\...\\Documents\\NetBeansProjects\\SeleniumIE\\IEDriverServer_Win32_3.14.0\\IEDriverServer.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
//commented lines do not work
//options.setCapability("NoNavBar", false);
//options.setCapability("toolbars", false);
visor = new InternetExplorerDriver(options);
visor.get("https://www.google.com/");
visor.quit();
}
}
You can use the Robot class to maximize the window by activating the F11 key. IE does not show any toolbars when it is maximized.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_F11);
r.keyRelease(KeyEvent.VK_F11)
Alternatively, Selenium does have capabilities that allow for browser maximization:
driver = new FirefoxDriver();
driver.manage().window().maximize();
You could send key stroke with F11 to make IE11 run in full screen.
Or I think kiosk mode can meet your requirement.
When you run Internet Explorer in Kiosk mode, the Internet Explorer title bar, menus, toolbars, and status bar are not displayed and Internet Explorer runs in Full Screen mode.
When you run iexplore -k page, IE will start in kiosk mode. You could check this thread and this blog for more information.
Related
I have a situation where I need to open a link incognito window from another url.
I have tried by using action class to perform control shift n key events but it's not working.
Robot class working fine but I'm looking for other alternative.
Actions act = new Actions(driver);
act. KeyDown(Keys.cONTROL);
act.keyDown(Keys.SHIFT);
act.keyDown("n").build(). perform ();
But it's not working
In case you want to open a new window in incognito mode i.e. without cookies you can simply open a regular new Selenium driver new window and the to clear the cookies with the following command:
driver.manage().deleteAllCookies();
I am trying to capture screenshots in a headless ChromeDriver instance. I can get a proper screenshot without running headless.
I have tried to just follow an example for this, however, the example is not working properly.
public static void main(String[] args) throws Exception{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.navigate().to("https://adobe.com");
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(srcFile, new File("testscreenshot.png"));
driver.quit();
}
The screenshot the code above generates will be blank white, with a scroll bar on the side. What can I do to capture a screenshot in headless mode? Is this possible?
This behavior was different on Windows, Ubuntu, and Mac. I am currently working on Mac. It was not rendering the page for some reason. This was all I needed before I took my screenshot
new WebDriverWait(driver, 10)
I am trying to click on cart icon on top right corner from Appium in chrome browser mobile.
Code to click :
driver.findElement(By.xpath("//a[#href='/viewcart']")).click();
URL : https://www.2gud.com/?cmpid=2G108229
Note: Please open this URL in mobile device and verify.
Error : Code is clicking somewhere else on mobile device.
This is working. Checked in Android 7.1 emulator
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
public class Demo {
public static WebDriver driver = null;
public static void main(String args[]) throws InterruptedException {
System.out.println("Launching the chrome driver ");
System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver40.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
driver = new ChromeDriver(options);
driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
driver.get("https://www.2gud.com/?cmpid=2G108229");
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
Thread.sleep(3000);
System.out.println(driver.getTitle());
driver.quit();
}
Try to click using mouse actions or JavaScript executor.
Try using the following code.
Xpath to find the view cart: //a[contains(#href,'viewcart')]
Executing a click via JavaScript has some behaviors of which you should be aware. If, for example, the code bound to the onclick event of your element invokes window.alert(), you may find your Selenium code hanging, depending on the implementation of the browser driver. That said, you can use the JavascriptExecutor class to do this. My solution differs from others proposed, however, in that you can still use the WebDriver methods for locating the elements.
// Assume driver is a valid WebDriver instance that has been properly instantiated elsewhere.
WebElement viewCart = driver.findElement(By.xpath("//a[contains(#href,'viewcart')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", viewCart);
You should also note that you might be better off using the click() method of the WebElement interface, but disabling native events before instantiating your driver. This would accomplish the same goal (with the same potential limitations), but not force you to write and maintain your own JavaScript.
I am looking for some solution, like this in Selenium WebDriver:
ChromeOptions options = new ChromeOptions();options.addArgument("--start-maximized");
So browser window should be maximized when test is executed.
I found a profile based solution for this problem, but it opens a lot of tabs, which is maybe caused by escape characters.
#Test
public void chromeWithProfileLaunch() throws Exception {
String profileDir = "--user-data-dir=\"c:\\Temp\\profile1\""; //should be different folder every time
String leanftChromeExtension = "--load-extension=C:\\Program Files (x86)\\HPE\\LeanFT\\Installations\\Chrome\\Extension"; //to load the LeanFT extension
String homePage = "www.google.com"; //the homepage to start with
new ProcessBuilder("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", profileDir, leanftChromeExtension, homePage)
.start();
Thread.sleep(2000); //wait for Chrome process to load
Browser openedBrowser = BrowserFactory.attach(new BrowserDescription.Builder().title("Google").type(BrowserType.CHROME).build());
Verify.areEqual(homePage, openedBrowser.getURL());
}
I don't know about maximized but LeanFT supports putting the browser in fullScreen mode.
LeanFT doesn't support maximize() out of the box yet.
However, you could use sendKeys() method.
I'm not entirely sure if you can to it on the browser instance directly, or you need to getPage() first, but you can definitelly send Super key (win key) + ↑ as specified here. ↓ for restoring back to the initial state.
Here's an example using sendKeys with Java SDK if you need it.
Below is my scenario :
Open the URL (http://google.com)
Press "F12" key
I have tried below lines of code :
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://google.com");
String CurrentURL= driver.getCurrentUrl();
System.out.println("Current URL is : " + CurrentURL);
Actions action = new Actions(driver);
action.sendKeys(Keys.F12);
System.out.println("successfuly pressed key F12");
driver.close();
}
It is printing "successfuly pressed key F12" on the console. But, I don't see 'F12' being pressed on website.
Please can anyone help me out of this ?
Thanks in advance.
I have been trying to use C# selenium to automatically open browsers with the devtools console open. To date (January 2020) My experience with C# is
Chrome options.AddArguments("--auto-open-devtools-for-tabs");
Firefox options.AddArgument("-devtools");
IE11 no command line options but you can use driver.FindElement(By.Id("body")).SendKeys(Keys.F12); after the browsers is open
Edge I have not been able to find any way to do this automatically, so selecting the browser and pressing F12 will have to suffice.
Thanks to the other contributors who helped me get this far
For pressing F12: The following Selenium Java code using Robot could work both in Firefox and Chrome:
driver.get("https://www.google.com/");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F12);
robot.keyRelease(KeyEvent.VK_F12);
VS 2017 , Selenium v 3.12.1 ,C# , Firefox V 60.0.2 , Chrome V 66 , Nunit v3.10.1 , Gecko Driver v 20.1 , chrome driver v 2.4
I tried to search for Firefox but did not success but I do get solution for Chrome v66
Please provide profile like this:
options.AddArguments("--auto-open-devtools-for-tabs");
This is the chrome driver implementation:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");
options.AddArguments("disable-infobars");
options.AddArguments("--disable-notifications");
options.AddArguments("--auto-open-devtools-for-tabs");
driver = new ChromeDriver(DrivePath, options, TimeSpan.FromSeconds(100));
you may have a look here as well: https://peter.sh/experiments/chromium-command-line-switches/
Below commands are NOT working, this is issue with Geckodriver so Gecko team has to provide some solution or fix for that :
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.F12);
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Perform();
Actions action = new Actions(driver); action .KeyDown(Keys.Control).SendKeys(Keys.F12).KeyUp(Keys.Control).Perform();
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Click();
I think you forgot to add the perform method.
So it should be:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12);
action.perform();
or
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).perform();
Can you try pressing F12 on body of the website? I used below java junit code and it opened google and pressed F12.
#Test
public void Test_Google_FireFox() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
baseUrl = "https://www.google.com";
driver.get(baseUrl);
driver.findElement(By.xpath("/html/body")).sendKeys(Keys.F12);
OR,
driver.findElement(By.cssSelector("body")).sendKeys(Keys.F12);
OR,
driver.findElement(By.tagName("body")).sendKeys(Keys.F12);
}