简体   繁体   中英

How to get the URL of the current window using Selenium WebDriver in C#?

In my application when sign-in, then it navigates to another page. Now I need to get that new URL using WebDriver in selenium C#.

I can't find any function to do this. I have tried driver.Url , driver.getLocation() and driver.getCurrentUrl() , but nothing is working in my C# application. So is it possible get the current URL somehow? After it gets navigated?

Yes, you can get the URL of the current page. Instantiate your driver and then get the driver's Url property.

Code snippet:

IWebDriver driver = new FirefoxDriver();
String currentURL =  driver.Url;

Help from: Selenium: Find the base Url

IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver("C:\\");
Console.WriteLine("url "+ driver.Url);

driver.Url gives you the current url

WebDriver driver = new WebDriver();
String currentURL =  driver.getCurrentURL();
This will give you current URL

Methods on this page don't work if tab is opened by javascript button. In that case you need to do somethign like this:

wait.Until(wd => wd.WindowHandles.Count == 2);

var handles = _driver.WindowHandles;

_driver.SwitchTo().Window(handles.Last());

Console.WriteLine(_driver.Url);

Switching to the tab by getting all window handles sets the URL value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM