Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract partial text from element link with python/selenium

In the below HTML, my goal is to return zzde7e35d-8d9d-4763-95d2-9198684abb12

<div class = container>    
    <a class="Blue-Button" data-type="patch" data-disable-with="Waiting" href="/market/opening/zzde7e35d-8d9d-4763-95d2-9198684abb12">Yes</a>
</div>

The problem is, I can’t even seem to locate the URL within the div

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

link = example.url
driver.get(link)
URL = driver.find_element_by_xpath('//a[contains(@href,"market")]')
print(URL)

Printing the above, I seem to get a bunch of random characters unrelated to the HTML at all, let alone the URL in question.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If it simplifies the issue, the number of characters that are returned will always be the same length, is indexing an easy work around?

>Solution :

If you want to get the href you need to use get_attribute('href')
this will give you /market/opening/zzde7e35d-8d9d-4763-95d2-9198684abb12 and then split() this and you will get the last element.

link = example.url
driver.get(link)
URL = driver.find_element_by_xpath('//a[contains(@href,"market")]')
print(URL.get_attribute('href').split("/")[-1])

Output:

zzde7e35d-8d9d-4763-95d2-9198684abb12
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading