如何使用 Python 从 Selenium 的重定向链中获取中间值 URL?
How can I get a intermediate URL from a redirect chain from Selenium using Python?
我在 Python API 和 Firefox 中使用 Selenium 来做一些自动的事情,这是我的问题:
- 单击原始页面上的 link,假设在页面 a.com
- 我被重定向到 b.com/some/path?arg=value
- 然后我立即再次被重定向到最终地址 c.com
那么有没有办法用Selenium URL b.com/some/path?arg=value Python API?我试过 driver.current_url
但是当浏览器打开 b.com 时,浏览器似乎仍在加载中并且仅当最终地址 时才返回结果c.com 已加载。
另一个问题是,有没有办法向 Selenium 添加一些事件处理程序,例如 URL-change? Phantomjs 有能力,但我不确定 Selenium。
is there a way to get the intermediate redirect URL b.com/some/path?arg=value with Selenium Python API?
我会使用 Explicit Wait 的轮询间隔较小。这个想法是等待初始页面上的正文元素过时:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
body = driver.find_element_by_tag_name("body")
wait = WebDriverWait(driver, 5, poll_frequency=0.05)
wait.until(EC.staleness_of(body))
print(driver.current_url)
您可能还需要减少页面加载超时:
driver.set_page_load_timeout(0.5)
Another question is that is there a way to add some event handlers to Selenium for like URL-change?
这正是这些显式等待的意义所在。有相关的 title_is
、title_contains
预期条件,很容易编写您的 custom one(例如,等待当前 URL 中的某个子字符串)。
可以在您的 Selenium 测试中设置 BrowserMob 代理等代理服务器,然后通过代理服务器路由您的 Web 流量。流量信息全部被捕获为HARfiles.You可以尝试通过插入代理服务器来获取这些信息,例如BrowserMob Proxy
AFAIK Selenium 提供的唯一监听机制是 EventFiringWebDriver wherein you can plugin your own event listening by extending AbstractWebDriverEventListener via the register method in EventFiringWebDriver. But the EventFiringWebDriver has limitations. It cannot eavesdrop into events that arise out of Actions class. There's an alternative to that as well. Sometime back I created a blog post that talks about it. Maybe you can refer that as well. Here's the link
我不知道 Python 中是否有与此类似的内容(因为我从未使用过 Selenium Python 绑定)
回答我自己的问题。
如果重定向链很长,可以考虑试试@alecxe和@Krishnan提供的方法。但在这种特定情况下,我找到了一个更简单的解决方法:
When the page finally landed c.com, use
driver.execute_script('return window.document.referrer')
to get the
intermediate URL
您可以从 performance
日志中获取重定向。根据 docs and 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
我在 Python API 和 Firefox 中使用 Selenium 来做一些自动的事情,这是我的问题:
- 单击原始页面上的 link,假设在页面 a.com
- 我被重定向到 b.com/some/path?arg=value
- 然后我立即再次被重定向到最终地址 c.com
那么有没有办法用Selenium URL b.com/some/path?arg=value Python API?我试过 driver.current_url
但是当浏览器打开 b.com 时,浏览器似乎仍在加载中并且仅当最终地址 时才返回结果c.com 已加载。
另一个问题是,有没有办法向 Selenium 添加一些事件处理程序,例如 URL-change? Phantomjs 有能力,但我不确定 Selenium。
is there a way to get the intermediate redirect URL b.com/some/path?arg=value with Selenium Python API?
我会使用 Explicit Wait 的轮询间隔较小。这个想法是等待初始页面上的正文元素过时:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
body = driver.find_element_by_tag_name("body")
wait = WebDriverWait(driver, 5, poll_frequency=0.05)
wait.until(EC.staleness_of(body))
print(driver.current_url)
您可能还需要减少页面加载超时:
driver.set_page_load_timeout(0.5)
Another question is that is there a way to add some event handlers to Selenium for like URL-change?
这正是这些显式等待的意义所在。有相关的 title_is
、title_contains
预期条件,很容易编写您的 custom one(例如,等待当前 URL 中的某个子字符串)。
可以在您的 Selenium 测试中设置 BrowserMob 代理等代理服务器,然后通过代理服务器路由您的 Web 流量。流量信息全部被捕获为HARfiles.You可以尝试通过插入代理服务器来获取这些信息,例如BrowserMob Proxy
AFAIK Selenium 提供的唯一监听机制是 EventFiringWebDriver wherein you can plugin your own event listening by extending AbstractWebDriverEventListener via the register method in EventFiringWebDriver. But the EventFiringWebDriver has limitations. It cannot eavesdrop into events that arise out of Actions class. There's an alternative to that as well. Sometime back I created a blog post that talks about it. Maybe you can refer that as well. Here's the link
我不知道 Python 中是否有与此类似的内容(因为我从未使用过 Selenium Python 绑定)
回答我自己的问题。
如果重定向链很长,可以考虑试试@alecxe和@Krishnan提供的方法。但在这种特定情况下,我找到了一个更简单的解决方法:
When the page finally landed c.com, use
driver.execute_script('return window.document.referrer')
to get the intermediate URL
您可以从 performance
日志中获取重定向。根据 docs and 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