[Python] Locate link that is not an anchor and does not have unique identifiers other than text

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have to log into a website and navigate to a specific link. The content on the site is dynamic and tends to move, so I don't really trust using an XPath. Also, the link is not in an anchor, does not have an ID, and has a generic class. As best as I can tell the only thing I can trust is the text field:

    <div class="prevent-select">
    <div></div>
    <span class="tyl-list-item__text menuTitleClass" tabindex="0" data-testid="hub__tylermenu__node__title" text="Employee Job/Salary" aria-describedby="tcw-tooltip-56">Employee Job/Salary</span>
    <tcw-tooltip id="tcw-tooltip-56" style="border: 0px; clip: rect(0px, 0px, 0px, 0px); height: 1px; margin: -1px; overflow: hidden; padding: 0px; position: absolute; width: 1px; outline: 0px; appearance: none;">Enterprise ERP&gt;Human Capital Management&gt;Payroll&gt;Employee Maintenance&gt;Employee Job/Salary</tcw-tooltip>
    </div>


    Do I just not understand how XPaths work -- is that the actual solution? I'm trying to use an XPath currently just to see it work and not having success. I'm inspecting the link and then selecting Copy XPath. Here's my code I'm using and the link is being sent to the url_search variable:

    #1. Go to Munis Login Page and enter username
    browser = webdriver.Chrome(options=options)
    browser.get("<website http>")

    user_val = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "idp-discovery-username")))
    user_val.send_keys(usr)
    user_val.send_keys(Keys.ENTER)

    #2. Enter Password on the next screen
    pwd_val = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "i0118")))
    pwd_val.send_keys(pwd)
    pwd_val.send_keys(Keys.ENTER)

    #3. Locate the Employee Job/Salary link and select it
    # To get to the URL follow the path: Select Human Capital Management --> Payroll --> Employee Maintenance --> Employee Job/Salary
    url_search = WebDriverWait(browser, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="FAV_f5284d90-9650-463b-aa03-67827413f2f6"]/tcw-list/tcw-expansion-panel/tcw-list/tcw-list-item[1]//div/div'))).click()

    Continue reading...

Compartilhe esta Página