Selenium 在网站重定向后立即获取当前 URL

huangapple go评论72阅读模式
英文:

Selenium get current URL as soon as the website is redirected

问题

我正在尝试在标签页的URL更改时立即获取网站的URL。但是当前脚本会等待页面加载,然后才提供URL。我想要实时获取URL,即使在页面加载之前也能获取。

iniurl = driver.current_url
一些自动化步骤
while url_change:
   if driver.current_url != ini_url:
       url_change = False
       print(driver.current_url)
       driver.quit()

目前这个方法非常慢,不是实时的。

英文:

I am trying to get the URL of a website as soon as the URL on tab changes. But currently the script waits for the page to load and then gives me the URL. I want to get the URL in real time even before the page gets loaded.

iniurl = driver.current_url
SOME AUTOMATION STEPS
while url_change:
   if driver.current_url != ini_url:
       url_change = False
       print(driver.current_url)
       driver.quit()

Currently this is very slow, not realtime.

答案1

得分: 0

我在一些测试之后终于找到了答案。这是实时的,不会等待页面加载,因此比使用页面加载策略='eager'更快。

iniurl = driver.current_url
一些自动化步骤
while url_change:
   if driver.execute_script("return window.location.href") != ini_url:
       url_change = False
       print(driver.current_url)
       driver.quit()
英文:

I found an answer to this finally after some testing. This is in real time and does not wait for page to load so its faster than using the page load strategy = 'eager'

iniurl = driver.current_url
SOME AUTOMATION STEPS
while url_change:
   if driver.execute_script("return window.location.href") != ini_url:
       url_change = False
       print(driver.current_url)
       driver.quit()

huangapple
  • 本文由 发表于 2023年6月1日 16:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定