skip to Main Content

The number of rows and sequence is not fix. It is random.. The attached image shown only two type of html row.

Type A row are 2,3,4,5,7,8,9,10,11,12,13,15, I would like to extract the data of class = "message-in focusable-list-item….." (highlighted in green).

Type B row are 1,6,11,14. I would like to extract tableindex = "-1".

enter image description here

I have tried using the following method to get the value from Type A row with given XPath. It worked.

msgInOutPath = '//*[@id="main"]/div[3]/div/div[2]/div[3]/div[8]/div/div'
msgInOut = wait.until(EC.presence_of_element_located((By.XPATH, msgInOutPath)))
classValue = msgInOut.get_attribute('class')
print(f'Class: {classValue}')

mainXPath = '//*[@id="main"]/div[3]/div/div[2]/div[3]'

The problem is Type A and Type B sequence is random. Number of rows also random.

  1. How can I check if the row is Type A or Type B under the main XPath and get the data I want?

Thank you in advance for all the pro tips!

2

Answers


  1. Use the main_x_path to find all the rows within that section. Loop through each row element. Inside the loop, use another XPath expression to check for specific identifiers of Type A or Type B rows.

    for i in range(1, len(driver.find_elements_by_xpath(main_x_path+ "/div")) + 1):
        row_xpath = maix_x_path + f"/div[{i}]"
        row_element = wait.until(EC.presence_of_element_located((By.XPATH, row_xpath)))
    
        # Check for Type A
        if i in [2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 15]:  # Adjust based on your list
            msgInOutPath = row_xpath + "/div/div"  # Assuming message element is nested within another div
            msgInOut = wait.until(EC.presence_of_element_located((By.XPATH, msgInOutPath)))
            classValue = msgInOut.get_attribute('class')
            print(f'Type A, Class: {classValue}')
        
        # Check for Type B
        elif i in [1, 6, 11, 14]:  # Adjust based on your list
            table_index_element = row_element.find_element_by_xpath(".//div[@tableindex='-1']") 
            table_index_value = table_index_element.text  .get_attribute('tableindex') if needed
            print(f'Type B, tableindex: {table_index_value}')
    
        # Handle cases where the row doesn't belong to either type
        else:
            print(f'Unidentified row type at position {i}')
    
    Login or Signup to reply.
  2. If you look at the HTML provided, you will see a pattern to the two types of DIVs that you are referring to.

    Type A looks like

    <div class role="row">
    

    Type B looks like

    <div class="_amjw _amk1 _aot1 focusable-list-item" tabindex="-1">
    

    So we can grab these DIVs, do a simple check to see if it contains the attribute "role", and that splits them into either Type A or B. From there you can grab the info you want.

    I would do it like this…

    wait = WebDriverWait(driver, 10)
    divs = wait.until(EC.visibility_of_all_elements_located(...))
    for div in divs:
        if div.get_attribute("role") == "row":
            # Type A
            class_value = div.find_element("div > div").get_attribute("class")
        else:
            # Type B
            table_index = div.get_attribute("tabindex")
    

    The part I can’t provide is the locator ... in the wait. You haven’t provided enough HTML to be able to determine that but basically you want a locator that pulls all the Type A/B DIVs into divs. From there, the rest should be straightforward.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search