【问题标题】:How to get next redirected url in selenium python?如何在 selenium python 中获取下一个重定向的 url?
【发布时间】:2021-11-25 19:18:24
【问题描述】:

我是 StackOverflow 的新手,所以我会尽力描述问题。

我在https://www.google.com/ 的页面上,你知道有搜索按钮。我输入了查询并使用 selenium 单击了搜索按钮。所以需要一些时间。 我想在完成搜索后获取 URL。 这意味着如何在页面加载后获取页面的 URL。因为让我解释一下……

  1. 包含验证码的页面。我必须填写并点击提交
  2. 如果验证码正确,则会在 URL 栏中打开另一个 URL。
  3. 但如果验证码有误,那么它将保留在同一个 URL 上
  4. 所以我想知道验证码是否有效
  5. 如果验证码有效,那么它肯定会转到下一个 URL。

我试过了:

    try:
        WebDriverWait(self.driver, 2).until(EC.url_changes('https://www.mystaticurl.com/aftercaptchafilled'))
        print('Captcha Accepted')
    except TimeoutException:
        print('[Captcha Error : Solving captcha code]')
        self.Fill_captcha_function()

但它不起作用。它仍然显示验证码已填充 请问有人吗?

提前致谢

【问题讨论】:

    标签: python selenium automation selenium-chromedriver


    【解决方案1】:

    加上Jayanth所说的,我个人使用了这种方法。

    current = driver.current_url
    failed = "https://www.google.com"
    if current == failed:
        print("Still same url!")
    if current != failed:
        print("New url!")
    

    你也可以

     driver.get("google.com")
     old = driver.current_url
     "captcha code here"
     new = driver.current_url
     if new == old:
         print("did not work :(")
     if new != old:
         print("YAY it worked :)")
    

    【讨论】:

      【解决方案2】:

      您的操作后,请使用以下方法获取当前网址,

      driver.current_url
      

      【讨论】:

      • 它有效。谢谢伙计。
      猜你喜欢
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2019-12-19
      • 2013-03-11
      相关资源
      最近更新 更多