How to pick up text, in CSS element receiving 2 class

Asked

Viewed 1,359 times

1

I need to collect text from an angular system, where the element receives 2 CSS classes

<div class="title ng-scope">80</div>

I tested several ways to capture the text, but it does not return (the commented lines shows the attempts)

Python code, using Phantomjs and Selenium below:

from selenium import webdriver
from datetime import datetime
driver = webdriver.PhantomJS()

try:
    url_reputacao = driver.get("http://rdash.github.io/#/")
    element = driver.find_elements_by_class_name('title ng-scope')
    #element = driver.find_element_by_css_selector("title ng-scope")
    #element = driver.find_elements_by_css_selector("div[class='title ng-scope']")
    #element = driver.find_element_by_css_selector("div.widget-icon green pull-left ng-scope > div.title ng-scope").text
    #element = driver.find_element_by_xpath("//div[contains(@class,'title ng-scope')]")

    print(element.text)
    driver.quit()

except Exception as erro:
    data = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    driver.get_screenshot_as_file('screenshot-%s.png' % data)
    driver.quit()

1 answer

1


Danilo,

better analyzing the page you spent on Chrome, at the inspecionar the item "80 Users", click on the tab Network and then in XHR and in JS, by redoing the request to the website (F5), I could understand where and when the data is passed to the template.

When entering the link Dashboard.html, we can find the value you want, without using the library Selenium (which speeds up and greatly the process).

inserir a descrição da imagem aqui

Code:

from bs4 import BeautifulSoup
import requests

url = 'http://rdash.github.io/templates/dashboard.html'
html_source = requests.get(url).text

soup = BeautifulSoup(html_source, 'html.parser')

users = soup.find('div', {'class':'title'})
print(users.text)

Output:

>>> 80

Obs.: Give a print(soup.prettify())) and look for the line <i class="fa fa-users"></i></div><div class=title>80</div><div class=comment>Users</div> to verify that this number 80 It’s really about users. Could also have used the i class fa fa-users to locate the value or the div with the text Users, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.