A
A
aab1372021-08-22 17:47:19
Python
aab137, 2021-08-22 17:47:19

Selenium. I use find_elements and get_atribute, but how can I do it in one request so that I don’t get a not attached error?

I do it like this:

dialogs = [i.get_attribute('data-list-id') for i in
                   self.find_elements_by_xpath('//ul[@id="im_dialogs"]/li[contains(@class, "nim-dialog_unread ")]')]

Here the not attached error can come out, how can I make it so that not an element, but an attribute is immediately located in one request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-22
@Vindicar

About not-attached - I suspect you didn't wait for the page to fully load.
In general, XPath has a choice of attribute values:

//ul[@id="im_dialogs"]/li[contains(@class, "nim-dialog_unread ")]/@data-list-id

Or perhaps so
string(//ul[@id="im_dialogs"]/li[contains(@class, "nim-dialog_unread ")]/@data-list-id)

Here string() is not a Python function, but an XPath function. If selenium supports it, then the query should return exactly the string value of the attribute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question