1

I have an HTML code like this:

<span class="class_name">10</span>
<span class="class_name">20</span>
<span class="class_name">32</span>

The number (10, 20, 32) are variable, they could be 32, 56 and 65.

In Selenium I do:

findclass = driver.find_element_by_class_name("class_name")

Now, my goal is take and assign number to variable like:

var1 = 10
var2 = 20
var3 = 32

Thank you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
JuConte
  • 513
  • 2
  • 7
  • 18

2 Answers2

2

Use find_elements_by_class_name. It will return a list of all elements with this class name.

findclass = driver.find_elements_by_class_name("class_name")

for i in findclass:

    print(i.text)

To find the second element in the list just use: print(findclass[1].text).

If you are looking for a specific value you can use CSS selector or XPath, you don't need the list of all the elements...

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • Not work: for i in findclass: TypeError: 'WebElement' object is not iterable – JuConte Aug 25 '19 at 11:48
  • @JuConte see the first line in the answer... you need to change the var `findclass` don't use: `findclass = driver.find_element_by_class_name("class_name")` use: `findclass = driver.find_elements_by_class_name("class_name")` see the diffrens? insted of finding one elenemt youll find all elements! – Moshe Slavin Aug 25 '19 at 12:27
  • perfect, it work! But now, how can I define the frist, the second and third value? I need to assign var1='first i.text', var2='second i.text?, ecc... – JuConte Aug 25 '19 at 12:47
  • @JuConte, as you can see `get_elements`, is a list so you can get any element within the list! what do you need? do you need a specific value? or location? – Moshe Slavin Aug 25 '19 at 13:02
  • I need all values, how can I define the position (or location)? – JuConte Aug 26 '19 at 05:26
  • I try to explaine well... in for loop (for i in findclass) i need to take only second value. There is a solution to define the position, for example print i.text(second value)? – JuConte Aug 26 '19 at 05:42
  • Just like any `list` you can get the position with `[]` it starts from `[0]` for the first one and `[1]` for the second... – Moshe Slavin Aug 26 '19 at 05:49
  • @JuConte see more about lists [here](https://www.w3schools.com/python/python_lists.asp) and [here](https://docs.python.org/3/tutorial/introduction.html#lists) – Moshe Slavin Aug 26 '19 at 05:56
  • solved! Whit enumerate(findclass) it work! thank you – JuConte Aug 26 '19 at 06:21
2

@MosheSlavin was was in the right direction. However to put all the values in a List you you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CLASS_NAME:

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "class_name")))])
    
  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.class_name")))])
    
  • Using XPATH:

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='class_name']")))])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352