Reputation: 11
Example: http://url1.com (javascript redirect) -> url2.com (javascript redirect new window) -> url3.com (javascript redirect) -> url4.com (urls not defined, i have tons of randoms urls)
My problem: i doesn't know how track redirects chain. I know first url1, i can catch last url with browser.current_url. But how catch url2,3 (and all other between url1 and last url).
With requests its easy:
r = requests.get("http://url1.com")
for ele in r.history:
print ele.url
print r.url
But requests doesnt work with Javascript.
What i can do?
Upvotes: 0
Views: 1777
Reputation: 345
As the first answer said, you can't do it with selenium
. However, you can combine selenium
with BrowserMobProxy
because webdriver options support BrowserMobProxy.
Also, I would advice you to try selenium-wire
, which extends Selenium’s Python bindings to give you access to the underlying requests made by the browser. You author your code in the same way as you do with Selenium, but you get extra APIs for inspecting requests and responses and making changes to them on the fly.
See selenium-wire
Upvotes: 1
Reputation: 20067
You cannot do that with Selenium.
A simplified explaination is the 301/302 status code redirects are handled by the browser on http level (again simplified - SE works with the DOM/the html of the page), and never propagated to Selenium. Substitute Selenium with javascript in the previous sentence, and it's still mostly true.
The best you could do with js is to inject an beforeunload
or unload
event listener to catch and acknowledge the existence of url1 -> url2 redirect, but that's about it.
The reason you can do it with python's requests
library is because it works on the lower (http) protocol level, and keeps track of them. As you have the solution for it already, why not just use it for this test.
Upvotes: 1