-1

so I'm really new to python and I wanna get my number of friends I got on a site named roblox for the sake of learning. This is where my code is right now:

 from selenium import webdriver
 ff = webdriver.FireFox()
 ff.get("https://www.roblox.com/home")
 element = driver.find_element_by_class("col-xs-12 section home-friends"[0])

where I am currently

I want to get the text "Friends (2)"

any ways of doing that?

2 Answers2

0

What you need is:

    text = driver.find_element_by_class("col-xs-12 section home-friends")
.find_element_by_tag_name("h3").text

If you have more than 1 , you can find the element with class = "container-header" first:

    text = driver.find_element_by_class("col-xs-12 section home-friends")
.find_element_by_class_name("container-header").find_element_by_tag_name("h3").text
Linh Nguyen
  • 1,120
  • 7
  • 13
  • from selenium import webdriver ff = webdriver.Firefox() ff.get("https://www.roblox.com/home") element = ff.find_element_by_class_name("col-xs-12 section home-friends"[0]).find_element_by_class_name("container-header"[0]).find_element_by_tag_name("h3").text print (element) input("hi") Is this how you do it? – Carlo Games Mar 29 '17 at 11:38
  • First, at find_element_by_class_name("container-head‌​er"[0]) - there should not be [0]. Second, make sure that you are logged in to the site – Linh Nguyen Mar 29 '17 at 12:01
  • There are 2 problems both in question and in answer: 1) there is no method `find_element_by_class()` in `selenium`, 2) Compound class names cannot be used in `find_element_by_class_name()` – Andersson Mar 29 '17 at 12:11
0

You cannot use compound class names with find_element_by_class_name() (not just ..by_class() but ..by_class_name() ! ), but you still can use it in CSS selector, like

ff.find_element_by_css_selector(".col-xs-12.section.home-friends")`

If you need to exctract "Friends (2)", try:

elem_text = ff.find_element_by_css_selector("div.col-xs-12.section.home-friends h3").text
Andersson
  • 51,635
  • 17
  • 77
  • 129