Chrome Browser Is Not Opening in Selenium Webdriver

Selenium webdriver is opening the browser, but not opening the given url

For anyone encountering this issue, please make sure that you have installed the chromedriver (or whatever driver is available for your browser). For chrome you first need to check your information and find out the version from chrome://version/
then from, https://sites.google.com/a/chromium.org/chromedriver/downloads
download the corresponding driver and copy the filepath into the the executable_path section.

Having some trouble when opening chrome browser with Selenium ChromeDriver

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


Related Topics



Leave a reply



Submit