检查浏览器中当前URL [英] Check what the current URL is in browser

查看:69
本文介绍了检查浏览器中当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Selenium库完成一项任务。任务是检查打开的浏览器中当前的URL,这不是Web浏览器,它是由Web驱动程序打开的。我的意思是用户打开的任何浏览器的当前URL。目前,我不确定是否可以这样做,但是我有一个几乎可以做到的代码,但这仅在使用Web驱动程序打开浏览器的情况下才可以。代码见下文。

I have a task of which I am trying to accomplish using the Selenium libraries. The task is to check what the current URL is in the open browser, I don't mean a web browser, which has been opened by the web driver. I mean the current URL of whatever browser the user has open. Currently I am not completely sure whether this is possible, but I have a code which almost does it, but that is only if the browser has been opened using the web driver. Code seen below.

WebDriver driver = new ChromeDriver();

driver.get("http://www.example.com");

String currentUrl = driver.getCurrentUrl();

System.out.println("The current URL is " + currentUrl);
Thread.sleep(5000);

driver.quit();

使用该代码,我想知道是否可以对其进行修改,以便驱动程序元素是用户打开了自己的当前版本,而不是实际的WebDriver?

Using that code I was wondering if it is possible to modify it, so that the driver element is the current one which the user has opened themselves and not the actual WebDriver?

因此,假设用户在www.google.com上运行了Chrome,那么该代码将获得URL www.google.com,因为这是当前的打开URL

So let's say the user has Chrome running on www.google.com, then the code would get the URL www.google.com as that is the current open URL in the Chrome browser.

推荐答案

您的驱动程序只能与自己的范围进行通讯。用户正在做什么,Driver无法看到。
除非我不能正确理解您的问题,否则您要问的是一个无效的问题。

Your driver is only able to communicate with its own range. What user is doing, Driver is not able to see that. Unless i have not understand your question correctly, what you are asking is an invalid question.

这篇关于检查浏览器中当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆