Search code examples
pythonseleniumgoogle-chromeimacros

PYTHON + SELENIUM (CHROME): How can I extract a specific text from my current url and use the extracted text to go to another?


I've been using iMacros for a long thing and I had a code where I could extract a text and use it to create a specific url I needed.

It was something like this:

#I can extract the XPath text with this.
TAG XPATH="//*[@id="meio"]/div[2]/section/article[5]/h2/span/a" EXTRACT=TXT

#With this, I'm able to use that extracted text to go to another url. I think it's obvious to say, but I can place my word wherever I want with "{{!EXTRACT}}".
URL GOTO=https://www.spiritfanfiction.com/perfil/{{!EXTRACT}}

There's anything like this I could use in Python?

Let's say this is my current url:

https://www.spiritfanfiction.com/perfil/depreciate

I need to extract "depreciate" and use it to go to this url:

https://www.spiritfanfiction.com/mensagens/post?usuario=depreciate

Ps: in this case, I can't extract directly from the XPath. It HAS to be in the current url.

Thank you.


Solution

  • Here is the code that will give you the desire url.

    newURL = driver.current_url.split(".com")[0]+".com/mensagens/post?usuario=" +driver.current_url.split("/")[-1]
    # navigate to newURL
    driver.get(newURL)