How to get text, in CSS element receiving 2 class

1

I need to collect text from an angled 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)

Code in python, 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()
    
asked by anonymous 14.04.2017 / 23:13

1 answer

1

Danilo,

by better analyzing the page you passed in Chrome , inspecionar item "80 Users" , click on the Network and then on XHR and JS , redo% to the site ( request ), I could understand where and when data is passed to the template.

When you enter the link dashboard.html , we can find the value you want, without using the library F5 (which speeds up the process a lot).

Code:

frombs4importBeautifulSoupimportrequestsurl='http://rdash.github.io/templates/dashboard.html'html_source=requests.get(url).textsoup=BeautifulSoup(html_source,'html.parser')users=soup.find('div',{'class':'title'})print(users.text)

Output:

>>>80

Note:GiveSeleniumandlookfortheprint(soup.prettify()))linetoseethatthis<iclass="fa fa-users"></i></div><div class=title>80</div><div class=comment>Users</div> number really refers to users. You could also have used 80 to find the value or i class fa fa-users with the text div , for example.

    
17.04.2017 / 13:36