How to get only ID on URL bar in selenium python

Issue

I have code like this:

if request.method == 'POST':
    form = SearchForm(request.POST)
    if form.is_valid():
        text = form['look'].value()
        chrome_options = Options()
        chrome_options.add_experimental_option(
            "excludeSwitches", ["enable-automation"])
        chrome_options.add_experimental_option(
            'useAutomationExtension', False)

        chrome_options.add_experimental_option("prefs", {
            "download.prompt_for_download": False,
            "safebrowsing.enabled": True
        })
        driver = webdriver.Chrome(
            ChromeDriverManager().install(), options=chrome_options)
        driver.maximize_window()
        driver.get(text)
        ...

Then I get a URL like this:
https://lovepik.com/image-610179440/cartoon-mothers-day-vector-material.html

But I want the returned result to only get the ID of 610179440, what should I do?

Solution

Use Below code

url = driver.current_url
id = url.split("image-", 1)[1].split("/")[0]
print(id)

Answered By – Ketan Pardeshi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published