0

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
4
  • Provide html structure so that we can help you. You can provide only the part with the table (div). Commented Nov 10, 2022 at 14:04
  • Thank-you so much for responding, I attached the whole Image :) Commented Nov 10, 2022 at 16:04
  • Do you get any errors. If yes, could you paste them here? What part of you code works as expected and at what line it doesn't? Or what result are you trying to get and what result are you actually getting? Commented Nov 10, 2022 at 19:20
  • I am getting this error : "EC.presence_of_all_elements_located((By.XPATH, "//div[@class='oxd-table-body']//div[@role='row']")))) AttributeError: 'bytes' object has no attribute 'presence_of_all_elements_located' -The answer I received, let's call it #Approach1. - I want to know if I can get using Looping statements . p.s: thank-you for response Commented Nov 11, 2022 at 12:46

1 Answer 1

1

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}")
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting this error : "EC.presence_of_all_elements_located((By.XPATH, "//div[@class='oxd-table-body']//div[@role='row']")))) AttributeError: 'bytes' object has no attribute 'presence_of_all_elements_located' -But I was able to learn how to locate elements of the table, thanks to your code, so thankyou.
Now I used your code, in a different python file so the error i'm getting is: File "X:\PythonProjectTraining\Day 19 Selenium\Stackoverflow help.py", line 30, in <module> total_disabled_users = len(WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located( File "X:\PythonProjectTraining\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: -Rest of the code, is fine, just the #get the requirements part there's something wrong
so finally, it worked, the problem was there was no disabled user in the table, so it was throwing exception, now it works, so thank you. I just want to know how I can achieve the similar result using loops? or do you suggest I should follow this method if it is optimal? Once again, thank-you
If you want to use it like a look you could do something like: for user in total_users: and then make click or whatever you want with the elements, for example clicking them with user.click()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.