如何使用Python从Selenium的重定向链中获取中间URL? [英] How can I get a intermediate URL from a redirect chain from Selenium using Python?

查看:475
本文介绍了如何使用Python从Selenium的重定向链中获取中间URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Selenium与Python API和Firefox结合使用来执行一些自动操作,这是我的问题:

I'm using Selenium with Python API and Firefox to do some automatic stuff, and here's my problem:

  1. 点击原始页面上的链接,例如在 a.com
  2. 页面上
  3. 我已重定向到 b.com/some/path?arg=value
  4. 然后我立即再次被重定向到最终地址 c.com
  1. Click a link on original page, let's say on page a.com
  2. I'm redirected to b.com/some/path?arg=value
  3. And immediately I'm redirected again to the final address c.com

有没有办法使用Selenium Python API获取中间重定向URL b.com/some/path?arg=value ?我尝试了driver.current_url,但是当浏览器在 b.com 上时,似乎浏览器仍在加载中,并且仅当加载了最终地址 c.com 时,结果才返回

So is there a way to get the intermediate redirect URL b.com/some/path?arg=value with Selenium Python API? I tried driver.current_url but when the browser is on b.com, seems the browser is still under loading and the result returned only if the final address c.com is loaded.

另一个问题是,是否可以将一些事件处理程序添加到Selenium中,例如进行URL更改? Phantomjs有能力,但是我不确定硒.

Another question is that is there a way to add some event handlers to Selenium for like URL-change? Phantomjs has the capacity but I'm not sure for Selenium.

推荐答案

回答我自己的问题.

如果重定向链很长,请考虑尝试提供的@alecxe和@Krishnan方法.但是在这种特定情况下,我发现了一个更简单的解决方法:

If the redirect chain is very long, consider to try the methods @alecxe and @Krishnan provided. But in this specific case, I've found a much easier workaround:

当页面最终登陆c.com时,请使用 driver.execute_script('return window.document.referrer')获取 中间网址

When the page finally landed c.com, use driver.execute_script('return window.document.referrer') to get the intermediate URL

这篇关于如何使用Python从Selenium的重定向链中获取中间URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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