[Python] Webdriver selenium on click function and switch to a different tab in azure to fetch...

Discussão em 'Python' iniciado por Stack, Junho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I want to fetch employee data into an excel from Azure portal using Python code. The landing page shows the list of employees, click each employee name, switch to properties tab, then fetch details displayed such as First name, Country, Job title. This should be done iteratively.

    Adding the image to make it clear. Here, URL takes me to the landing page. It has several employee names and they are links. Once clicked, it takes to the page2 which has 2 tabs - Overview and Properties. Once clicked onto Properties tab, it displays the data that we need to fetch. This has to be done recursively for each employee. [​IMG]

    What the code does now - Connects to the Azure portal successfuly and lands on the page that has the employee list. The click() function is not working and it does not switch to properties tab. Can anyone please suggest what can be modified for the code to work?

    The error I get now is -> selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='ms-Pivot-text text-135'][normalize-space()='Properties']"}

    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()

    Continue reading...

Compartilhe esta Página