0

I am trying to automate a process using python and the selenium library.

The item I need to click on is: "In charge LIFE - Individual-risk recruitment".

This is the entire hierarchical structure of the HTML starting with the table id

<table id="contentArea" border="0" cellpadding="0" cellspacing="0" xpath="1">
<tbody>
   <tr>
      <td class="leftNavBg" style="width: 150px">
         <table border="0" cellspacing="0" cellpadding="0" id="leftNav" statusfield="ctl00_mb_menu1_status">
<tbody>
   <tr></tr>
   <tr class="leftNavBg3" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg1" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg1" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg1" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg3" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg1" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block" childnum="6"></tr>
   <tr class="leftNavBg2" liv="2" style="DISPLAY: none"></tr>
   <tr style="DISPLAY: none" liv="2"></tr>
   <tr class="leftNavBg2" liv="2" style="DISPLAY: none"></tr>
   <tr style="DISPLAY: none" liv="2"></tr>
   <tr class="leftNavBg2" liv="2" style="DISPLAY: none"></tr>
   <tr style="DISPLAY: none" liv="2"></tr>
   <tr class="leftNavBg1" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg3" style="DISPLAY: block"></tr>
   <tr style="DISPLAY: block"></tr>
   <tr class="leftNavBg1" style="DISPLAY: block">
      <td></td>
      <td valign="top"></td>
      <td colspan="3" class="navText" align="left" style="width:133px;">
         <a class="navText" href="#" onclick="switch_menu(this, 6, 2);return false;">
         In charge LIFE - Individual-risk recruitment.
         </a>
      </td>
   </tr>

Can anyone help me with this?

Thank you all.

I try this:

try:
    element = driver.find_element(By.XPATH, "//table[@id='contentArea']//table[@id='leftNav']//tr[@class='leftNavBg1']//td[contains(@class, 'navText') and contains(text(), 'In charge LIFE - Individual-risk recruitment')]")
    print("Element found:", element)
except NoSuchElementException:
    print("Element not found")
JJDouglas
  • 1
  • 1

1 Answers1

0

To locate the element with text as In charge LIFE - Individual-risk recruitment you can use the following locator strategy:

  • Using XPATH:

    element = driver.find_element(By.XPATH, "//table[@id='contentArea']//table[@id='leftNav']//tr[@class='leftNavBg1']//td[@class='navText']/a[contains(., 'In charge LIFE - Individual-risk recruitment')]")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    

Ideally to locate a visible element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategy:

  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@id='contentArea']//table[@id='leftNav']//tr[@class='leftNavBg1']//td[@class='navText']/a[contains(., 'In charge LIFE - Individual-risk recruitment')]")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

To click on a clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solution:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[@id='contentArea']//table[@id='leftNav']//tr[@class='leftNavBg1']//td[@class='navText']/a[contains(., 'In charge LIFE - Individual-risk recruitment')]"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Even with the code you suggested, I can't get the element to click. '''WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[@id='contentArea']//table[@id='leftNav']//tr[@class='leftNavBg1']//td[@class='navText']/a[contains(., 'In charge LIFE - Individual-risk recruitment')]"))).click()''' – JJDouglas Jun 12 '23 at 21:46