4

I'm using Selenium and Python to extract information from a page.

Here is the div I want to extract from:

<div class="_2v66">5</div>

I want to extract the value "5".

Here is the Python I have written:

element = browser.find_elements_by_class_name('_2v66').getText

print('Views:')
print(element)

When I run this script, I get the following message:

Traceback (most recent call last):
  File "<string>", line 95, in <module>
AttributeError: 'list' object has no attribute 'getText'
[Finished in 15.478s]

Solution:

Although I originally thought the div class was unique, after a closer inspection of the page I realized that it is not a unique div and therefore the solution was as follows:

browser.get(('https://www.facebook.com/example_page_1/insights/?section=navVideos'))

browser.implicitly_wait(60)

# find_elements_by_class_name - Returns the div in which the metrics are found
elements = browser.find_elements_by_class_name('_2v66')

for e in elements:
    print(e.text)

The browser.implicitly_wait was crucial to allow the page to load. The errors I was receiving with regards to the object no existing were because of that.

3 Answers 3

7

Use just .text

element = browser.find_element_by_class_name('_2v66').text

If there are multiple elements, you'll have to loop through them.

elements = browser.find_elements_by_class_name('_2v66')
for e in elements:
    print(e.text)
Sign up to request clarification or add additional context in comments.

3 Comments

It's still giving me " File "<string>", line 94, in <module> AttributeError: 'list' object has no attribute 'text' "
Check the two statements, if you use find_elements it returns a list whereas find_element will return the single element
It is still giving me an error saying "unable to locate element"
3

As per the HTML you have provided to extract the text 5, instead of using find_elements* you need to use find_element and you can use the following solution:

element = browser.find_element_by_class_name('_2v66').text
print(element)

Note A: The Selenium-Python client doesn't have any method getText, but instead text.

Note B: You have to ensure that this particular <div> tag can be uniquely identified through the class attribute _2v66.

Comments

0

For newer versions of selenium (docs):

element = driver.find_element(By.CLASS_NAME,'css-449o1m')
print(element.text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.