-1

I am automating steps in a browser using Selenium/Python; however, I am stuck with trying to understand how to get Selenium to recognize a text value in a table and click on the URL in the corresponding row. The scenario we're trying to duplicate is a user logs into the website and searches for a login by username, and at that point the results will return in the UI. The results will be in a table like this, and we need to be able to find the Username and click on the User_ID which is clickable link to the user admin page.

User_ID Username Company_Name Name User_Status

Here is the HTML:

<tr onclick="window.location='?viewUsers=1&amp;loginId=99999&amp;customerId=1&amp;token=9999999999999999999999'" onmouseover="onRowOver(this);" onmouseout="onRowOut(this);" style="cursor: pointer;">
                                            
                   <td class="bg8 resultTableRowBorderBottom">
                    <div class="font1 trNumbersAndDates">
                        <!---->
                                    <a href="?viewUsers=1&amp;loginId=99999&amp;customerId=1&amp;token=9999999999999999999999">
                            99999
                            </a>
                                <!---->
                        </div>
                    </td>
                  <!--  -->
                    <td class="bg8 resultTableRowBorderLeft   resultTableRowBorderBottom">
                    <div class="font1 trStrings">      [email protected]
  </div>
                     </td>
                  <!--  -->
                    <td class="bg8 resultTableRowBorderLeft   resultTableRowBorderBottom">
                    <div class="font1 trStrings">      Company Name
  </div>
                     </td>
                  <!--  -->
                    <td class="bg8 resultTableRowBorderLeft   resultTableRowBorderBottom">
                    <div class="font1 trStrings">      First Name
  </div>
                     </td>
                  <!--  -->
                    <td class="bg8 resultTableRowBorderLeft   resultTableRowBorderBottom">
                    <div class="font1 trStrings">      Status
  </div>
                     </td>
                  <!--  -->
                 </tr>

Here is the code that I have so far for this piece... this is the line that I'm struggling with

click_user = driver.find_element_by_xpath("//tr[text() = '" + user_login.email + "']//a")

The user_login.email variable represents the [email protected] in the html that is being passed in when the script kicks off.

    click_user = driver.find_element_by_xpath("//tr[text() = '" + user_login.email + "']//a")
    click_user.click()
    timeout = 5
    try:
        element_present_3 = ec.presence_of_element_located((By.NAME, 'finish'))
        WebDriverWait(driver, timeout).until(element_present_3)
    except TimeoutException:
        print("Timed out waiting for element to load")
2
  • What's the error
    – PDHide
    Commented Nov 14, 2020 at 10:34
  • as per the htlm provided there is no child element "a" what element you want to get
    – PDHide
    Commented Nov 14, 2020 at 11:19

1 Answer 1

1

Xpath can find the previous sibling of any field in the DOM.

Try using xpath like this:

"//div[text() = '" + user_login.email + "']/preceding-sibling::div/a"

Not the answer you're looking for? Browse other questions tagged or ask your own question.