Dona bhatt
Dona bhatt

Reputation: 71

Having some trouble when opening chrome browser with Selenium ChromeDriver

I use Selenium ChromeDriver to open chrome browser and load a site into it programmatically.

I install selenium & ChromeDrive from this NuGet

Install-Package Selenium.WebDriver -Version 3.141.0 Install-Package Selenium.WebDriver.ChromeDriver -Version 77.0.3865.4000

I have some questions:

  1. if target pc has no chrome browser installed then how can i capture it by ChromeDriver ? is it possible?

  2. when i am opening chrome browser by ChromeDriver instance then browser is opening chrome browser with a site but another CUI window is getting opened which i do not want to visible or i want to hide this CUI window. if it is not possible then how could i open this CUI window in minimize state?

    a sample CUI window screen shot attached below when i work with FirefoxDriver. the same occur when i work with ChromeDriver instance.

    enter image description here

  3. when i executing this code chromeDriver.Close(); then opened chrome browser is getting closed but CUI window is still open. so if i click 5 times on open button then 5 CUI window is getting open along with 5 chrome browser instance which i had to close manually ....which i do not want to manually close it rather i want to close it when browser will be closed....how to achieve it ?

  4. how to capture from code that opened chrome browser is close by this code chromeDriver.Close(); or if user click on cross button of chrome browser to close it?

  5. how to open a new tab in already opened chrome browser instead of opening new chrome browser instance. if no chrome browser is open at all then new chrome browser will be open...how to achieve it by code. this below code opening new chrome browser always....what to change there for my point 5

    chromeDriver = new FirefoxDriver(options);
    chromeDriver.Navigate().GoToUrl("https://www.google.com");
    
  6. another issue occur when i work with chrome driver that. it open chrome browser but a notification appear on browser like Chrome is being controlled by automated test software

    I search google to hide it and found people said to use this option

    options.setExperimentalOption("excludeSwitches", new String[] { "enable-automation" });
    

    at my end this function does not available setExperimentalOption so what to do?

Please answer point wise with sample code.

Upvotes: 1

Views: 1294

Answers (2)

Amir Azad
Amir Azad

Reputation: 123

For question 2,3 you can use below code (use DriverService.Dispose(); to manually dispose driver service) :

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MyProject
{
    public class Browser : IDisposable
    {
        bool disposed = false;    
        IWebDriver Driver;

        public Browser()
        {
            //Chrome Driver copied on startup path                
            ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(Application.StartupPath, "chromedriver.exe");

            //hide driver service command prompt window
            driverService.HideCommandPromptWindow = true;
            ChromeOptions options = new ChromeOptions();

            //hide browser if you need              
            //options.AddArgument("headless");
            //or this to hiding browser
            //options.AddArgument("--window-position=-32000,-32000");

            //On offer Dona bhatt for disable automated test notification
            options.AddExcludedArgument("enable-automation");
            //options.AddArgument("disable-infobars");

            Driver = new ChromeDriver(driverService, options);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
            {
                Driver.Close();
                Driver.Quit();
                Driver.Dispose();

                DriverService.Dispose();
            }

            disposed = true;
        } 

        //this method for navigation
        public string Navigate(string url)
        {
            string page = string.Empty;
            try
            {
                Driver.Navigate().GoToUrl(url);
                page =Driver.PageSource;
            }
            catch
            {

            }
            return page;
        }

        //this method for wait to an element be visible by element ID
        private void WaitUntilLoad(string id, int timeOut)
        {
            WebDriverWait waitForElement = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut));
            try
            {
                waitForElement.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
            }
            catch (WebDriverTimeoutException e)
            {

            }
        }
     } 
 }

Use this class:

using(Browser brw=new Browser())
{
    string pageSource=brw.Navigate("My URL");
}

Upvotes: 2

Zakk Diaz
Zakk Diaz

Reputation: 1093

I couldn't post this as a comment because it's too long. I think this question is off topic but I just noted my comments on your items...

This seems to be much to broad. Are you having a problem with any specific issue? All of these seems like things you can research and find out.

1) The chrome driver has to be downloaded and shipped with your app.

2) It might be possible to hide that window but not sure why that would be a hard requirement. I'm going to just say that you can't by default prevent the window from showing.

3) You're going to have to close the chrome windows through your .net code. Selenium isn't going to be graceful enough to terminate all the chrome windows.

4) I'm not sure what you're asking for. What's the problem?

5) I think tabs might be done very differently between browsers, it might not be something natively supported in selenium. There might be some chrome driver commands that can facilitate but I have no idea what they are.

6) Again, this is typically not the preferred experience, I'm guessing one of your packages has a different featureset than whatever you're reading was using.

Upvotes: 0

Related Questions