简体   繁体   English

如何在 C# 中使用 Selenium WebDriver 获取当前窗口的 URL?

[英]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#.现在我需要在 selenium C# 中使用 WebDriver 获取新的 URL。

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.我尝试过driver.Urldriver.getLocation()driver.getCurrentUrl() ,但在我的 C# 应用程序中没有任何效果。 So is it possible get the current URL somehow?那么是否有可能以某种方式获取当前 URL? After it gets navigated?导航之后呢?

Yes, you can get the URL of the current page.是的,您可以获取当前页面的 URL。 Instantiate your driver and then get the driver's Url property.实例化您的驱动程序,然后获取驱动程序的Url属性。

Code snippet:代码片段:

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

Help from: Selenium: Find the base Url帮助来自: Selenium:查找基本 URL

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

driver.Url gives you the current url driver.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.如果选项卡是通过 javascript 按钮打开的,则此页面上的方法不起作用。 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通过获取所有窗口句柄切换到选项卡设置 URL 值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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