How to locate a block with certain text using python selenium

Question:

With selenium in python, I want to collect data about a user called "GrahamDumpleton" on the website below:
https://github.com/GrahamDumpleton/wrapt/graphs/contributors

And this is the block I want to locate with the user name "GrahamDumpleton":
enter image description here

How to locate this block using selenium?
Thank you.

Asked By: KDWB

||

Answers:

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')]")
Answered By: Prophet