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".
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.
- 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
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.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
Type B looks like
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…
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 intodivs
. From there, the rest should be straightforward.