Home > front end >  Python Selenium (Getting Values from specific table)
Python Selenium (Getting Values from specific table)

Time:11-12

Image of the table I want to use

So I wanted to get a specific value of the table, from a particular row and column, but there's no <table> in the inspect sheet, and I can't seem to find a way to retrieve my required result.

My requirement is: Checking how many users are there and how many are enabled/disabled

The XPATH that I have given below might be wrong, because I tried various XPATH configurations nothing worked, so maybe I am doing something wrong

Please check my code below and help me or guide me how can I solve this, thank you.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
driver.maximize_window()

driver.find_element(By.XPATH, "//input[@placeholder='Username'\]").send_keys("Admin")
driver.find_element(By.XPATH, "//input[@placeholder='Password'\]").send_keys("admin123")
driver.find_element(By.XPATH, "//button[@class='oxd-button oxd-button--medium oxd-button--main orangehrm-login-button']").submit()

time.sleep(3)
driver.find_element(By.XPATH, "/html\[1\]/body\[1\]/div\[1\]/div\[1\]/div\[1\]/aside\[1\]/nav\[1\]/div\[2\]/ul\[1\]/li\[1\]/a\[1\]").click()

rows = len(driver.find_elements(By.XPATH, "(//div\[@class='oxd-table-card'\])"))
print("Total Number Of Rows:"   rows)
    
count = 0
for r in range(1, rows   1):
    status = driver.find_element(By.XPATH,"(//div[@role='row'])[2]").text 
    status = driver.find_element(By.XPATH, "//div[@class='orangehrm-container']").text
    status1 = driver.find_element(By.XPATH, "(//div\[contains(text(),'Disabled')\])").text
    
    if status == "Enabled":
        count = count   1
    else:
        if status1 == "Disabled":
            count = count   1
    
print("Total Number of Users:"   rows)
print("Total Number of Enabled Users:"   count)
print("Total Number of Disable Users:"   (rows - count))
    
driver.quit()

As I said we have 3 requirements:

  • Checking how many users we have
  • Checking how many of then are disabled
  • Checking how many users are enabled

CodePudding user response:

Is this what you expect?

# Needed libs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# We create the driver
driver = webdriver.Chrome()
driver.maximize_window()

# We navigate to the url
url='https://opensource-demo.orangehrmlive.com/web/index.php/auth/login'
driver.get(url)

# We make login
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "username"))).send_keys("Admin")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "password"))).send_keys("admin123")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//button"))).click()

# Navigate to desired section
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[@href='/web/index.php/admin/viewAdminModule']"))).click()

# Get the requirements
total_users = len(WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='oxd-table-body']//div[@role='row']"))))
total_enabled_users = len(WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='oxd-table-body']//div[@role='row']/div[5]/*[text()='Enabled']"))))
total_disabled_users = len(WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='oxd-table-body']//div[@role='row']/div[5]/*[text()='Disabled']"))))
print(f"Total users: {total_users}")
print(f"Total enabled users: {total_enabled_users}")
print(f"Total disabled users: {total_disabled_users}")
  • Related