我要使用的表格的影像
所以我想從特定的行和列中獲取表的特定值,但<table>檢查表中沒有,而且我似乎無法找到檢索所需結果的方法。
我的要求是:檢查有多少用戶以及啟用/禁用多少用戶
我在下面給出的 XPATH 可能是錯誤的,因為我嘗試了各種 XPATH 配置都沒有奏效,所以也許我做錯了什么
請檢查我下面的代碼并幫助我或指導我如何解決這個問題,謝謝。
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()
正如我所說,我們有 3 個要求:
- 檢查我們有多少用戶
- 檢查其中有多少被禁用
- 檢查啟用了多少用戶
uj5u.com熱心網友回復:
這是你所期望的嗎?
# 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}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/532184.html
上一篇:錯誤driver.find_element或find_elements
下一篇:Selenium無法獲取此類
