0

I need to transform the return of the URL I'm extracting into a valid element.

the code captures the URLs and then enters each one of them to extract data from the page

terminal exit enter image description here

terminal error enter image description here

links = []
classe = driver.find_elements(By. XPATH, "//*[@class='LinksShowcase_UrlContainer__kMj_n']/p")
for i in classe:
    sleep(0.5)
    links.append(i)
    print(links)
    sleep(2)
for linkAtual in links:
    driver.get(linkAtual)

I cannot share the link, as it is a platform that needs to create an account and be accepted, but the link is as text within the TAG 'P', follow the image of the page

enter image description here enter image description here enter image description here

1
  • 1
    Link should be attached the anchor tag with href attribute. Please post the relevant HTML or share the page link. Commented Dec 29, 2022 at 16:46

1 Answer 1

1

find_elements method return a list of WebElement objects.
These are not links (strings).
WebElement is a reference, a pointer to physical web element on the web page.
WebElement may containg href attribute that normally contains some link.
As mentioned by KunduK anchor elements are normally containing links, not p tag elements.
So, in case elements you collecting are containing links you can extract these links from the WebElement objects and use them later.
I can't debug this code since you did not share a link to page you working on as well as you did not share all your Selenium code, but I guess something like following can work:

links = []
classe = driver.find_elements(By. XPATH, "//*[@class='LinksShowcase_UrlContainer__kMj_n']/p")
for i in classe:
    link = i.get_attribute("href")
    print(link)
    links.append(link)
for linkAtual in links:
    driver.get(linkAtual)

UPD
In your case it is not href attribute but a text content. So, you can simply extract the text as following:

links = []
classe = driver.find_elements(By. XPATH, "//*[@class='LinksShowcase_UrlContainer__kMj_n']/p")
for i in classe:
    link = i.text
    print(link)
    links.append(link)
for linkAtual in links:
    driver.get(linkAtual)
Sign up to request clarification or add additional context in comments.

9 Comments

I edited the text and attached the page images
OK, I see. Did my suggestion worked? If not what is the problem?
the suggestion didn't work because it doesn't have the HREF attribute the link is as text inside the TAG P
Ah, sure, I see. So, you can simply extract the text there. I will edit my answer now. Let me know if it worked now.
ok, everything is fine, code working
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.