P
P
PesyCorm2021-01-08 23:10:41
Python
PesyCorm, 2021-01-08 23:10:41

Parsing html with Selenium+Python, incorrect return?

Hello! Difficulty encountered when trying to get references from hrefelement attributes a.

Link to the page in the repository on the git. (Pre-moderation swears, apparently because of the resource domain)

I used the find_elements_by_class_name method, and it returned (for each element)
<selenium.webdriver.remote.webelement.WebElement (session="45a0063f8da9f78a78c38b201240c24a", element="6b43db73-2b1a-4886-9237-f493f7693539")>
. Accordingly, hrefI can not get from it. Tell me, what could be wrong? Perhaps there is some other way to get links from elements?
Full code on git .
Thanks for the answer!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-01-09
@PesyCorm

Accordingly, I can’t get href from it

What you got is called class, and that's ok, you can't find href unless you
find it <div>with class testCard. It divdoes not and never has the attribute href.
5ff98d78357b2611397432.jpeg
Specifically, in this case, he only has class.
If you are searching hreffrom a nested divelement а,
5ff98daca53cf162828278.jpeg
then you first need to find elements for each found divelement a, then take them from them href.
for el in slide_elems:
    # Находим вложенный тег <a>
    tag_a = el.find_element_by_tag_name('a')
    print(tag_a.get_attribute('href'))

S
ScriptKiddo, 2021-01-09
@ScriptKiddo

The href attribute is only on the anchor, which is under testCard.
5ff8c7ac07093325434776.png
XPath for it:
//*[@class='testCard']/a
Here you write a string representation of a list with WebElement elements to a file. And you need to write the 'href' attribute.

def parse(self):
    self.go_to_questsions_page()
    slide_elems = self.driver.find_elements_by_class_name("testCard")
    f = open("text.txt", "w")
    f.write(str(slide_elems))
    for el in slide_elems:
      print(el.get_attribute('href'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question