2

I am using python, selenium and other packages as per my need. I am creating GUI(Graphical User Interface) where it will show total number of people passes threw the gate. we already have hardware which directly report to particular website which is written in php and database are sql and mariadb.

<span class="info-box-number f28 fc-666" id="tot_count">11</span>

This is the sample of code where, I want to capture '11' from this. I tried with 'id', 'class' and 'xpath' to capture this with .text but so far I can't find the solution. I refer some other questions but it didn't help me.

  • So, Idea is to create GUI interface which updates every second and show the number of people passes. I do have to get some other values with it which also, in similar format, same database with different class, id and xpath.

Note: All the class and ids are unique which I want to capture.

I am open to suggestion if someone has better idea, your suggestion would be appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
kmpatel100
  • 108
  • 5

1 Answers1

1

To print the text 11 you can use either of the following Locator Strategies:

  • Using class_name and get_attribute("textContent"):

    print(driver.find_element_by_class_name("info-box-number").get_attribute("textContent"))
    
  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element_by_css_selector("span.info-box-number#tot_count").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element_by_xpath("//span[contains(., 'info-box-number') and @id='tot_count']").text)
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CLASS_NAME and get_attribute("textContent"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "info-box-number"))).get_attribute("textContent"))
    
  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.info-box-number#tot_count"))).text)
    
  • Using XPATH and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'info-box-number') and @id='tot_count']"))).get_attribute("innerHTML"))
    
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352