Home > front end >  Get the link inside href selenium python
Get the link inside href selenium python

Time:10-06

I am trying to get the link href with python and selenium. The href url is <a target="_blank" href="https://click.discord.com/ls/click?upn=RANDOMNUMBERSANDLETTERS" style="text-decoration:none;line-height:100%;background:#5865f2;color:white;font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:15px;font-weight:normal;text-transform:none;margin:0px;"> Verify Email </a> I want to get the full discord link but I have no idea how, I tried looking for answers but all of them said element not found.

CodePudding user response:

You can have a web element using xpath

//a[contains(text(),'Verify Email')]

and the call

get_attribute('href')

sample code :

link = driver.find_element_by_xpath("//a[contains(text(),'Verify Email')]").get_attribute('href')
print(link)

CodePudding user response:

Try this one:

ele=driver.find_element_by_xpath("//a[contains(text(),'Verify Email')]")
ele.get_attribute('href')

print(ele)

CodePudding user response:

You could also use css locators since xpaths should generally be used as a last resort (they can break easier and mess up your browser automation, they are just more brittle). As a very basic example css=#email (or whatever the id is called), If you want to use a tag with id the syntax is an example would be from what you posted above is css=a[href='linkText'].

  • Related