Home > other >  How to find text on a webpage and copy the text in the adjacent section
How to find text on a webpage and copy the text in the adjacent section

Time:12-13

I need to find a location on the webpage by the visible text. Then I need to copy the value that is adjacent to it. It makes more sense when you see the screenshot. Screenshot So the idea is to find the text "Net PPW" and then grab the value, which for this example is "1.56".

I assume this is possible since they are in the same section of the webpage but I have no clue how to do this or where to start.

CodePudding user response:

One of the simpliest solution is mannually check every p element text on page to get next argument with p tag in result(cause they always stay together).

elements = driver.find_elements(By.TAG_NAME, 'p')

for x in range(len(elements)):
     if elements[x].text == "Net PPW"
         print(elements[x 1].text)

CodePudding user response:

With use of XPath you can locate the parent element based on it's parent and then locate the parent element, as following:

driver.find_element(By.XPATH, "//div[contains(@class,'justify-content-between')][contains(.,'Net PPW')]//p[contains(@class,'MuiTypography-body1')]").text
  • Related