Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Selenium Chromedriver opens wrong url onload

I'm using Selenium with chromedriver to scrape a list of data using HTML agility pack but when I try to navigate to this page, the url gets changed from url1 to url2. Therefore is there a way to stop this from happening?

 string url = "";
 Console.WriteLine("Web Crawler!");

 Console.WriteLine("Enter URL :");
 url = Console.ReadLine();
 Console.WriteLine("Entered URL :" + url);

 // selenium section
 ChromeOptions chromeOptions = new ChromeOptions();
 chromeOptions.AddUserProfilePreference("profile.default_content_setting_values.geolocation", 2);
 IWebDriver driver = new ChromeDriver(".", chromeOptions);
 driver.Navigate().GoToUrl(url);
like image 554
LordDraagon Avatar asked Dec 06 '25 05:12

LordDraagon


1 Answers

The url might not being passed correctly from the CMD. Instead, you can read it from a CSV file

using (TextFieldParser parser = new TextFieldParser("csvFile"))
{
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    string url = string.Empty;
    while (!parser.EndOfData) 
    {
        string[] fields = parser.ReadFields();
        url = fields[0];
    }
}
like image 129
Guy Avatar answered Dec 08 '25 17:12

Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!