I'm using the C# Selenium and Edge webdriver on Windows 10. Below is my code for switch between tab.
public class BrowserTest
{
private readonly EdgeDriver driver;
public BrowserTest()
{
EdgeDriverService service = EdgeDriverService.CreateDefaultService(configuration[DriverPathKey]);
service.SuppressInitialDiagnosticInformation = true;
service.HideCommandPromptWindow = true;
driver = new EdgeDriver(service, new EdgeOptions()
{
PageLoadStrategy = PageLoadStrategy.Default,
});
}
public void OpenGoogleAndDuckduckgo()
{
driver.Url = "https://google.com/";
driver.ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last()); // This line lead to Edge focusing
driver.Url = "https://duckduckgo.com/";
}
}
But seem it focusing Edge browser that taking me away from checking my e-mail or etc.
How can I get Selenium to "silently" focus on a new tab (or window etc) so I can do something while my code runs?
P/s: I tried for headless mode but I need to manual input credentials to login so I need a UI. Or if there a way to turn from window mode to headless mode in runtime?
You can not achieve that (change focus without focus) in Selenium. If you are running Selenium tests, just use different machine. HW or virtual.
Headless mode of browser is not a good solution also. Sooner or later you will have too much problems to solve with differences between normal mode and headless mode.
Avoiding captcha is very hard in automation mode of browser. You need AI to solve captcha in that mode and it is even not guaranteed it will works (depends on captcha and server protection).
If you need to test or grab some pages which has this protection you have to use different approach: Write addon for browser (javascript app) and run it in regular mode of browser.
I think you should use the same code but consider deploying your script in VM. with that you can continue to do your stuff in your local wherein your selenium code will get executed in Virtual machine (With your own OS setup )
Oracle VM virtual Box provides very feasible solution with respect to virtual machines. Check out their official web sites linked. They have different distribution for MacOS, Linux and Windows operating system.
Related
im looking example how can i start my normal google chrome in webdriver c#?
For now i use :
ChromeDriver driver;
public ChromeDriverService chromeDriverService;
chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("disable-infobars");
chromeOptions.AddExcludedArgument("enable-automation");
chromeOptions.AddAdditionalCapability("useAutomationExtension", false);
driver = new ChromeDriver(chromeDriverService,chromeOptions);
But it run my chromedriver.exe installed inside project. Can i just run my simple installed chrome? Without download any chromedriver.exe? It's important for me , because some website check if there is opened chromium with chromedriver.exe.
Is it possible to do that?
If you want not just open it but also perform some interaction with your site then you have to deal with Selenium and WebDriver.
You can try Python and undetected-chromedriver which has some workarounds preventing some systems to detect that your browser is running under webdriver control.
Unfortunately, it's the chromedriver.exe that lets you interact with Chrome so, without the driver, you wouldn't be able to interact with the actual browser.
If your issue is with chromium, try driving Mozilla with GeckoDriver
Selenium always uses chromedriver.exe for interacting with Chrome, I think you mean you want to load your default data directory. You can add a chrome option for this purpose:
chromeOptions.AddArguments(#"C:\Users\<your user name>\AppData\Local\Google\Chrome\User Data")
Take care, any other chrome must not be running that time with the same data directory. Like you may have run for your personal use.
You can also create a data directory somewhere else if you just want to show some realistic behavior to a website. But some websites are smart enough so it does not work every time.
I have tried
options.add_argument("--headless")
driver = new ChromeDriver(options)
it's working fine but my requirement is to hide it after initialization.
As you can see here: How to execute tests with selenium webdriver while browser is minimized there is no way for the browser to be minified.
You could in theory move the browser out of view as described here https://sqa.stackexchange.com/questions/15484/how-to-minimize-the-browser-window-which-was-launched-via-selenium-webdriver
driver.manage().window().setPosition(new Point(-2000, 0))
Chrome headless is an instance of chrome. You can't start from normal mode and switch to headless mid test. Once you start testing with any kind of browser mode, you need to stick to it. There are some work arounds I've not tried with opening windows with code and continuing from the new window.
Use phantomJSDriver for Headless automation
WebDriver driver = new PhantomJSDriver();
Since geckodriver v0.16.0 flashplayer is disabled by default.
Is there any possibility to start firefox with enabled flashplayer?
I'm using C#. My code right now:
var profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile("selenium"); //created firefox user named selenium
profile.SetPreference("plugin.state.flash", 1);
Code bellow doesn't work for me:
profile.SetPreference("dom.ipc.plugins.enabled.libflashplayer.so", true);
When I use this one:
profile.SetPreference("plugin.state.flash", 1);
firefox is asking if I want to enable flashplayer, and than refreshes page (with all inputs filled previously - so i got empty fields).
If I select "allow and remember", next time I start this code nothig is saved. I'm getting the same situation.
I found solution there:
How to enable Adobe Flash in FireFox Selenium webdriver with FirefoxProfile
If I replaced profile.setPreference("plugin.state.flash", 1); to profile.setPreference("plugin.state.flash", 2); it stopped to asking me if I want to enable flash player.
Here is the solution for you:
With Selenium 4.3.0, gecko driver v0.16.0 & Mozilla Firefox 53.0 this code works well with myfreecams.com.
It is worth mentioning that the default Firefox profile is not very automation friendly. When you want to run automation reliably on a Firefox browser it is advisable to make a separate profile. Automation profile should be light to load and have special proxy and other settings to run good test. You should be consistent with the profile you use on all development and test execution machines. If you used different profiles everywhere, the SSL certificates you accepted or the plug-ins you installed would be different and that would make the tests behave differently on the machines.
So, Create a New Firefox profile and use the same in the Test script involves three steps process. First you need to Start the Profile Manager, second is to Create a New Profile and third is to use the same profile in Test scripts. Let's assume we created a new FirefoxProfile by the name "debanjan".
Use the following code to open http://www.myfreecams.com/ with your new FirefoxProfile "debanjan":
String driverPath = "C:\\Utility\\BrowserDrivers\\";
//Mozila Firefox
System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
FirefoxDriver driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.navigate().to("http://www.myfreecams.com/");
PS: This code is in Java so you may have to convert it into C# format.
Let me know, if this helps you.
I want to take a screenshot of a webpage using Selenium.
I have notice that the action to take the screenshot require to open the web browser itself.
tried to change webDriver.Navigate().GoToUrl("http://www.google.com"); with webDriver.Url = "http://www.google.com"; but no success,
I even tried to leave it with no url and the browser opened with url of 'data', which now I understand that something else makes the browser to be open.
private void button1_Click(object sender, EventArgs e)
{
var capabilitiesInternet = new
OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.
SetCapability("ignoreProtectedModeSettings", true);
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("http://www.google.com");
Screenshot screenshot = ((ITakesScreenshot)webDriver).GetScreenshot();
screenshot.SaveAsFile("E:\\ScreenShot.png",
System.Drawing.Imaging.ImageFormat.Png);
webDriver.Quit();
}
No - you need to let the WebDriver request the page, otherwise how can it know what screenshot to produce?
If you're trying to avoid a real, 'slow' browser starting up and opening a window, you should either consider running that browser headlessly, as per:
How do I run Selenium in Xvfb?
Or check out the headless WebKit browser PhantomJS (or maybe SlimerJS), and using almost exactly the same WebDriver API as you have now, ask it to produce your screenshots 'in-memory':
Phantomjs - take screenshot of a web page
Just replace:
WebDriver webDriver = new ChromeDriver();
with:
WebDriver webDriver = new PhantomJSDriver();
(Obviously requires the application to be installed locally)
Edit: Just a note that the typical use-case for this is 'overnight' continuous-integration / continuous-testing when run from headless CI servers. However, it can be very easily added to other work-flows, e.g. for visual regression-testing, and simple one-off checks.
There's multiple ways to go about it:
There is chrome flag --no-startup-window which should open chrome without window. I tried it, and it didn't work, however, you can give it a go.
Use hacks - there is a way to start chrome without you ever seeing it, it works nicely. Just use --window-position=-9999,0, note that, TakeScreenShot() has some weird anomalies in my experience - it focuses the window, it runs out of memory randomly, etc. What I ended up doing is I wrote chrome extension which can take screenshots and return them back to selenium through JS.
I am using selenium WD in C# for cross browser testing but facing a strange problem that when ever i run my test using Nunit firstly Firefox window will open & then my desired browser window will open & run the test on it(desired browser).
As per my knowledge if any system is not having Firefox installed in it then it fails the script.
So is there any way to change this default value of browser in selenium.
I am able to run tests on different browser, my problem is only that before opening my desired browser by default first system is opening firefox. which create issue for me & my tests.
public void SetupTest()
{
driver = new SafariDriver();
baseURL = "http://google.com/";
verificationErrors = new StringBuilder();
}
Most probably, somewhere in your code you are initializing the Firefox Driver. Search for this within you code:
new FirefoxDriver();
You could also debug to the line
driver = new SafariDriver();
and see if it has an assigned value already.
But I am also pretty certain that you are initializing a FirefoxDriver somewhere.