Webdriver selenium 单击功能并切换到 azure 中的不同选项卡以使用 Python 获取员工数据

问题描述 投票:0回答:1

我想使用 Python 代码从 Azure 门户将员工数据提取到 Excel 中。登陆页面显示员工列表,单击每个员工姓名,切换到属性选项卡,然后获取显示的详细信息,例如名字、国家/地区、职位。这应该迭代地完成。

代码现在的作用 - 成功连接到 Azure 门户并登陆包含员工列表的页面。 click() 函数不起作用,并且不会切换到属性选项卡。谁能建议修改哪些内容才能使代码正常工作?

我现在得到的错误是 -> selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to located element: {"method":"xpath","selector":"//span[@class=' ms-Pivot-text text-135'][normalize-space()='属性']"}

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
import time
import openpyxl

# Create a new Excel workbook and select the active worksheet
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.append(["Name", "Country", "Job Title"])

# Set up Chrome WebDriver (make sure to download the chromedriver executable)
driver = webdriver.Chrome()

# Navigate to the target URL
driver.get(url)

wait = WebDriverWait(driver, 100)
driver.implicitly_wait(50)
names=driver.find_elements(By.XPATH, "//span[@id='abcgridcol6']//div[@class='azc-grid-headerlabel'][normalize-space()='Name']")
# Iterate through each name, click on it, and extract necessary information
for name in names:
    wait = WebDriverWait(driver, 100)
    driver.implicitly_wait(50)
    name.click()  # Click on the name to view its properties
properties_tab = driver.find_element(By.XPATH, "//span[@class='ms-Pivot-text text-135'][normalize-space()='Properties']")
    properties_tab.click()

    # Extract first name, country, and job title
    first_name = driver.find_element(By.XPATH, "//label[@for='PropertyItem71']").text
    country = driver.find_element(By.XPATH, "//div[contains(text(),'Country')]/following-sibling::div").text
    job_title = driver.find_element_by_xpath("//div[contains(text(),'Job Title')]/following-sibling::div").text
    sheet.append([first_name, country, job_title])
    
    # Go back to the previous page to continue looping
    driver.back()

# Save the Excel workbook
workbook.save("employee_data.xlsx")

# Close the WebDriver session
driver.quit()
python selenium-webdriver webdriver
1个回答
0
投票

我可以帮忙!

如果您能够分享您正在使用的 URl,那就更容易了。不过我建议使用类或标签来查找元素。例子

#this will return all the buttons on the page
buttons = driver.find_element(By.TAGNAME, 'button') 

for button in buttons:
    if button.Text == 'Properties'
        properties_Button = button
        properties_Button.click

如果您需要单击多个按钮,那么您应该单击包含按钮的 类并再次迭代它们,如果我们有 URL 链接,那么我可以为其编写更详细的代码。我希望这有助于快乐编码! :)

© www.soinside.com 2019 - 2024. All rights reserved.