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?
It seems Selenium doesn't switch automatically to this tab.
But you can do it manually
last = driver.window_handles[-1]
driver.switch_to.window(last)
print(driver.current_url)
Doc: Working with windows and tabs | Selenium
Other method:
Because every link looks like <a href="javascript:void(0);" onclick="SiteUrlFunc(6124);">7502 Colonial</a>
(with number 6124
) and when I click it then it opens new tab with url which has the same number 6124
- so
you can also generate this url without opening tab
link = driver.find_element(By.XPATH, f'(//a[contains(text(), "Double E")])[{1}]')
onclick = link.get_attribute('onclick')
print('on_click:', onclick)
number = onclick.replace("SiteUrlFunc(", "").replace(");", "")
print('number:', number)
print(f"https://www.solrenview.com/SolrenView/mainFr.php?siteId={number}")
Full working 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("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 = "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}]'))))
print(f"{driver.current_url = }")
# --- generate link ---
link = driver.find_element(By.XPATH, f'(//a[contains(text(), "Double E")])[{1}]')
onclick = link.get_attribute('onclick')
print('onclick:', onclick)
number = onclick.replace("SiteUrlFunc(", "").replace(");", "")
print('number:', number)
print(f"generated: https://www.solrenview.com/SolrenView/mainFr.php?siteId={number}")
# --- switch to last tab in browser ---
last = driver.window_handles[-1]
driver.switch_to.window(last)
print(f"last : {driver.current_url = }")
# --- switch to first tab in browser ---
first = driver.window_handles[0]
driver.switch_to.window(first)
print(f"first: {driver.current_url = }")
# ---
input("Press ENTER to close")
Result:
Program name: main.py
Checking Browser driver...
driver.current_url = 'https://www.solrenview.com/'
onclick: SiteUrlFunc(5746);
number: 5746
generated: https://www.solrenview.com/SolrenView/mainFr.php?siteId=5746
last : driver.current_url = 'https://www.solrenview.com/SolrenView/mainFr.php?siteId=5746'
first: driver.current_url = 'https://www.solrenview.com/'
Press ENTER to close