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.
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