Home > Mobile >  How to retrieve a url attribute with Python selenium?
How to retrieve a url attribute with Python selenium?

Time:08-01

Within this page - enter image description here

in your page. Just to get the URL you can either use string manipulation methods or you will have to use regex to parse the string.

Here is how you can do it.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"
import re

s = Service(chrome_path)
driver = webdriver.Chrome(service=s)
url = 'https://sourceforge.net/projects/mingw/files/latest/download'
driver.get(url)

jscode = driver.find_element(By.XPATH, "//noscript").get_attribute('innerHTML')
print(re.search(r'url=(.*)\?', jscode).group(1))

which gives us the output :

https://downloads.sourceforge.net/project/mingw/Installer/mingw-get-setup.exe
  • Related