[Python] Get current url of second tab using selenium?

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I try to get the current url using the following code -

    import time
    import os, sys
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By

    print(f"Program name: {os.path.basename(__file__)}")
    TRIAL = True
    BREAK_OUT = 10
    SAVE_INTERVAL = 1
    WAIT = 1
    path = os.path.abspath(os.path.dirname(sys.argv[0]))


    print(f"Checking Browser driver...")
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument('--use-gl=swiftshader')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("start-maximized")
    options.add_argument('--log-level=3')
    options.add_argument('--enable-unsafe-swiftshader')

    srv=Service()
    driver = webdriver.Chrome (service=srv, options=options)
    waitWD = WebDriverWait (driver, 10)

    link = f"https://www.solrenview.com/"
    driver.get (link)
    waitWD.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='gMapsFr']")))
    driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//label[@for="ByInstaller"]'))))
    time.sleep(0.5)
    waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="searchBox"]'))).clear()
    time.sleep(0.5)
    waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="searchBox"]'))).send_keys("Barrier Solar Inc.")
    time.sleep(0.5)
    driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="autoSuggestionsList"]//li'))))
    time.sleep(3)
    driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, f'(//a[contains(text(), "Double E")])[{1}]'))))

    currentURL = driver.current_url

    print(currentURL)
    input("Press!")


    As you can see when running the code a second tab is opening with the url

    https://www.solrenview.com/SolrenView/mainFr.php?siteId=5746


    is opening.

    When i run the program i allways only get this output:

    https://www.solrenview.com/


    How can i get this url from the second (active) tab?

    Continue reading...

Compartilhe esta Página