Home > database >  How to locate a block with certain text using python selenium
How to locate a block with certain text using python selenium

Time:11-28

With selenium in python, I want to collect data about a user called "GrahamDumpleton" on the website below: enter image description here

How to locate this block using selenium?
Thank you.

CodePudding user response:

This can be clearly done with XPath since XPath is the only approach supporting locating elements based on their text content.
So, that user block element can be located with the following XPath:

//li[contains(@class,'contrib-person')][contains(.,'Graham')]

In case you want only the header part of that block this XPath can be used:

//h3[contains(@class,'border-bottom')][contains(.,'Graham')]

So, Selenium code returning those elements can be correspondingly

driver.find_elements(By.XPATH, "//li[contains(@class,'contrib-person')][contains(.,'Graham')]")

And

driver.find_elements(By.XPATH, "//h3[contains(@class,'border-bottom')][contains(.,'Graham')]")
  • Related