我正在使用Selenium和Python API和Firefox来做一些自动化的事情,这是我的问题:
那么有没有办法用Selenium Python API获得中间重定向URL b.com/some/path?arg=value呢?我尝试了driver.current_url
,但当浏览器使用b.com时,浏览器似乎仍在加载中,只有在加载了最终地址c.com时才返回结果。
另一个问题是,有没有办法向Selenium添加一些事件处理程序,以便进行类似URL的更改?Phantomjs有这个能力,但我不确定是否有Selenium。
发布于 2017-02-14 03:35:26
您可以从performance
日志中获取重定向。根据docs和github answer的说法,这是我在C#中所做的,应该可以移植到Python中:
var options = new ChromeOptions();
var cap = DesiredCapabilities.Chrome();
var perfLogPrefs = new ChromePerformanceLoggingPreferences();
perfLogPrefs.AddTracingCategories(new string[] { "devtools.network" });
options.PerformanceLoggingPreferences = perfLogPrefs;
options.AddAdditionalCapability(CapabilityType.EnableProfiling, true, true);
options.SetLoggingPreference("performance", LogLevel.All);
var driver = new ChromeDriver(options);
var url = "https://some-website-that-will-redirect.com/";
driver.Navigate().GoToUrl(url);
var logs = driver.Manage().Logs.GetLog("performance"); //all your logs with redirects will be here
循环通过logs
,如果message.params.redirectResponse.url
等于原始URL,则message.params.request.url
将包含重定向URL。
发布于 2016-02-24 11:49:14
可以将代理服务器(如BrowserMob Proxy )设置到Selenium测试中,然后通过代理服务器路由web流量。当HAR files.You可以尝试通过插入BrowserMob Proxy等代理服务器来获取此信息时,将捕获所有交通信息
AFAIK Selenium提供的惟一侦听钩子机制是EventFiringWebDriver,您可以通过EventFiringWebDriver中的register方法扩展AbstractWebDriverEventListener来插入您自己的事件侦听。但EventFiringWebDriver有其局限性。它不能窃听发生在Actions类中的事件。除此之外,还有另一种选择。不久前,我写了一篇关于它的博客文章。也许你也可以参考一下。这是link
我不知道Python中是否有类似的东西(因为我从未使用过Selenium Python绑定)
https://stackoverflow.com/questions/35592602
复制相似问题