Pressing "F12" key directly through Selenium - java

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);
}

Related

Remove address bar in IE with selenium

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.

Click is happening some other place on Cart ICON in MSITE from appium code - Chrome browser

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.

Missing or invalid type argument for pointer action - Selenium

I am getting below mentioned error message while run program.
Error: Missing or invalid type argument for pointer action.
I am trying to click on sub menu which will display after mouse hover on main menu.
Code below:
public class ActionKeywords {
WebDriver driver = new FirefoxDriver();
#Test
public void openBrowser(){
driver.get("https://www.levissima.it/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#Test
public void verify_Menus(){
WebElement mainMenu = driver.findElement(By.xpath("//ul[#id='menu-main']/li/a"));
WebElement subMenu = driver.findElement(By.xpath("//a[contains(text(),'Impegno Per La Natura')]"));
Actions action = new Actions (driver);
action.moveToElement(mainMenu).perform();
action.click(subMenu).perform();
}
}
Please assist!
With Selenium 3.4.0 to work with Mozilla Firefox browser 53.x you need to download the latest geckodriver from here. Save it in your machine & provide the absolute path of the geckodriver. This code executes fine with some simple tweak to your own code.
WebDriver driver;
#BeforeTest
public void setup()
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
}
#Test
public void openBrowser(){
driver.get("https://www.levissima.it/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#Test
public void verify_Menus(){
WebElement mainMenu = driver.findElement(By.xpath("//ul[#id='menu-main']/li/a"));
WebElement subMenu = driver.findElement(By.xpath("//a[contains(text(),'Impegno Per La Natura')]"));
Actions action = new Actions (driver);
action.moveToElement(mainMenu).perform();
action.click(subMenu).perform();
}
The output is:
PASSED: openBrowser
PASSED: verify_Menus
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
Let me know if this answers your question.
One better way to achieve this click() would be :
Actions action = new Actions (driver);
action.moveToElement(mainMenu).moveToElement(subMenu).click().build().perform();
This is called chaining of actions.
I got that error today, and solved it just using another geckodriver version. In that case (Firefox 52 + Selenium 3.8.0 ), geckodriver 0.15 solved it.
Is not the first time im forced to download and try different versions of Firefox/Geckodriver/Selenium.
Downloading the latest geckodriver will not solve problems by defaults. You may need an older version according to your Firefox and geckodriver versions, so, don't hesitate to try on your own instead of just get the latest version hoping it will magically work.
I really recommend try different geckodrivers before become crazy.

Unable to Drag and Drop for web driver in Firefox

Below is the code i tried
WebDriver driver = new FirefoxDriver();
driver.get("http://www.w3schools.com/html/html5_draganddrop.asp");
Actions builder = new Actions(driver);
WebElement src = driver.findElement(By.id("drag1"));
WebElement des = driver.findElement(By.id("div2"));
builder.clickAndHold(src).build().perform();
builder.moveToElement(des).build().perform();
builder.release(des).build().perform();
driver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
I don't see the drag and drop operation happening, Although no error is returned in the console.
Am I missing any step here ?
Please give a try with following:
builder.dragAndDrop(src, des).build().perform();
If above doesn't help you, see if following helps:
http://elementalselenium.com/tips/39-drag-and-drop

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .
I am using latest selenium webdriver with java.
I have tried
sendKeys("s"),
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");
driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);
None of these work even after adding wait after each of the steps.
Any suggestions?
Thanks.
Update :-
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("s");
driver.findElement(By.xpath("//table[#class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
driver.findElement(By.name("btnG")).click();
Update 2 : -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
WebElement query = driver.findElement(By.name("destination"));
query.sendKeys("s");
Update 3 :-
I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
I believe you are testing auto-suggest here (not auto-complete)
Steps I follow -
Enter something in the input field
Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
Verify the text in the input field is same as expected.
This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");

Categories