Tarayıcı Ayarları
Tarayıcı Ayarları
Tarayıcı Ayarları
Komut Satırı Penceresinin Gizlenmesi
var firefoxService = FirefoxDriverService.CreateDefaultService();
firefoxService.HideCommandPromptWindow = true;
var chromeService = ChromeDriverService.CreateDefaultService();
chromeService.HideCommandPromptWindow = true;
Tarayıcı Penceresinin Gizlenmesi
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(BrowserOptionNames.ChromeHeadless);
..
new ChromeDriver(chromeService, chromeOptions);
var firefoxOptions = new FirefoxOptions();
firefoxOptions.AddArguments(BrowserOptionNames.FirefoxHeadless);
..
new FirefoxDriver(firefoxService, firefoxOptions);
İndirilen Dosya Konumu Belirleme
var firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference(“browser.download.dir”, Directory.GetCurrentDirectory() + “\\firefoxTemp”);
...
new FirefoxDriver(firefoxService, firefoxOptions);
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference(“download.default_directory”, Directory.GetCurrentDirectory() + “\\chromeTemp”);
...
new ChromeDriver(chromeService, chromeOptions);
Tarayıcı time out süresi ayarlama
Default olarak timeout süreleri; For implicit waits: 0 seconds. For page loads: 300 seconds. For script timeouts: 30 seconds.
Aşağıdaki kod ile değiştirilebilir.
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(1);
Driver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(1);
Driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromMinutes(1);
Tarayıcı Penceresi gizli(headless) olduğunda dosya indirme
Tarayıcı headless durumunda olduğunda dosya indirme işlemini yapamamaktadır. Aşağıdaki düzenlemenin yapılması gerekmektedir. Chrome için;
static async Task AllowHeadlessDownloadChrome(ChromeDriverService driverService, IWebDriver Driver)
{
var jsonContent = new JObject(
new JProperty("cmd", "Page.setDownloadBehavior"),
new JProperty("params",
new JObject(new JObject(
new JProperty("behavior", "allow"),
new JProperty("downloadPath", Directory.GetCurrentDirectory() + BrowserOptionNames.ChromeTemp)))));
var content = new StringContent(jsonContent.ToString(), Encoding.UTF8, "application/json");
var sessionIdProperty = typeof(ChromeDriver).GetProperty("SessionId");
var sessionId = sessionIdProperty.GetValue(Driver, null) as SessionId;
using (var client = new HttpClient())
{
client.BaseAddress = driverService.ServiceUrl;
await client.PostAsync("session/" + sessionId + "/chromium/send_command", content);
}
}
Firefox için;
static async Task AllowHeadlessDownloadFirefox(FirefoxDriverService driverService, IWebDriver Driver)
{
var jsonContent = new JObject(
new JProperty("cmd", "Page.setDownloadBehavior"),
new JProperty("params",
new JObject(new JObject(
new JProperty("behavior", "allow"),
new JProperty("downloadPath", Directory.GetCurrentDirectory() + BrowserOptionNames.FirefoxTemp)))));
var content = new StringContent(jsonContent.ToString(), Encoding.UTF8, "application/json");
var sessionIdProperty = typeof(FirefoxDriver).GetProperty("SessionId");
var sessionId = sessionIdProperty.GetValue(Driver, null) as SessionId;
using (var client = new HttpClient())
{
client.BaseAddress = driverService.ServiceUrl;
await client.PostAsync("session/" + sessionId + "/geckodriver/send_command", content);
}
}
Chrome ve Firefox için metodun kullanımı;
protected IWebDriver SetBrowser(string browserName)
{
if (browserName.Equals(BrowserOptionNames.Firefox))
{
var firefoxService = FirefoxDriverService.CreateDefaultService();
firefoxService.HideCommandPromptWindow = true;
var firefoxOptions = new FirefoxOptions();
firefoxOptions.AddArguments(BrowserOptionNames.FirefoxHeadless);
firefoxOptions.SetPreference("browser.download.folderList", 2);
firefoxOptions.SetPreference("browser.download.useDownloadDir", true);
firefoxOptions.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;application/vnd.ms-excel;application/vnd.ms-excel.addin.macroenabled.12;application/vnd.ms-excelsheet.binary.macroenabled.12;application/vnd.ms-excel.template.macroenabled.12;application/vnd.ms-excel.sheet.macroenabled.12");
firefoxOptions.SetPreference("pdfjs.disabled", true);
firefoxOptions.SetPreference(BrowserOptionNames.FirefoxDownload, Directory.GetCurrentDirectory() + BrowserOptionNames.FirefoxTemp);
Driver = new FirefoxDriver(firefoxService, firefoxOptions);
Task.Run(() => AllowHeadlessDownloadFirefox(firefoxService, Driver));
}
else if (browserName.Equals(BrowserOptionNames.Chrome))
{
var chromeService = ChromeDriverService.CreateDefaultService();
chromeService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(BrowserOptionNames.ChromeHeadless);
chromeOptions.AddUserProfilePreference(BrowserOptionNames.ChromeDownload, Directory.GetCurrentDirectory() + BrowserOptionNames.ChromeTemp);
Driver = new ChromeDriver(chromeService, chromeOptions);
Task.Run(() => AllowHeadlessDownloadChrome(chromeService, Driver));
}
return Driver;
}
Last updated
Was this helpful?