C# and Selenium.WebDriver: How to get the current URL

moondaddy 911 Reputation points
2021-08-13T23:56:40.843+00:00

I'm using the Selenium.WebDriver to do some work in web pages and things are working OK. One of my new tasks are to click on something in the web page and it navigates to a to a new page which I will scrape. This also works.

EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
EdgeDriver edgeDriver = new EdgeDriver(edgeOptions);
edgeDriver.Navigate().GoToUrl(@"https://www.SomeDoman.com/");

Along with scraping the page, I need to get the current URL which has changed since using the above code to open the page. I can't see any property for this.

How can I get the page's current URL

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,390 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,311 Reputation points Microsoft Vendor
    2021-08-16T03:06:47.627+00:00

    @moondaddy , you could use EdgeDriver.Url Property to access the current URL.

    I make a simple winform app and you could have a look.

    private void button1_Click(object sender, EventArgs e)  
            {  
      
                textBox1.Text = driver.Url;  
      
            }  
            EdgeDriver driver = new EdgeDriver();  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                 
                driver.Navigate().GoToUrl("https://learn.microsoft.com/en-us/dotnet/csharp/");  
                textBox1.Text = driver.Url;  
                 
            }  
    

    Result:

    123417-animation1.gif


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.