問題描述
我正在使用 Selenium 和 Python API 和 Firefox 來做一些自動的事情,這是我的問題:
I'm using Selenium with Python API and Firefox to do some automatic stuff, and here's my problem:
- 點擊原始頁面上的鏈接,比如說在頁面 a.com
- 我被重定向到 b.com/some/path?arg=value
- 我馬上又被重定向到最終地址 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 有能力,但我不確定 Selenium.
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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!