Home > Blockchain >  How to get only ID on URL bar in selenium python
How to get only ID on URL bar in selenium python

Time:07-03

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?

CodePudding user response:

Use Below code

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