How to find an element under located header using python selenium

Question:

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

Following the previous question, I located the header including the name "Graham" by finding XPath:

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

How could I find an element under this located header?
The XPath is:

//*[@id="contributors"]/ol/li/span/h3/span[2]/span/div/a

Thank you.

Asked By: KDWB

||

Answers:

The element you looking for can be uniquely located by the following XPath: //a[contains(.,'commit')].
So, if you want to locate directly all the commit amounts of users on the page this can be done as following:

commits = driver.find_elements(By.XPATH, "//a[contains(.,'commit')]")
for commit in commits:
    print(commit.text)

And if you want to locate the commits amount per specific user when you already located the user’s block or header element as we do in the previous question, this can be done as following:

header = driver.find_elements(By.XPATH, "//h3[contains(@class,'border-bottom')][contains(.,'Graham')]")
commit = header.find_element(By.XPATH, ".//a[contains(.,'commit')]")
print(commit.text)

Pay attention.

  1. Here header.find_element(By.XPATH, ".//a[contains(.,'commit')]") we applied find_element method on header web element object, not on the driver object.
  2. We use a dot . at the beginning of the XPath to start searching from the current node (header), not from the beginning of the entire DOM.

UPD
addition can be located with this XPath: //span[@class='cmeta']//span[contains(.,'++')] and deletion with //span[@class='cmeta']//span[contains(.,'--')]

Answered By: Prophet

Xpath expression (//*[@class="border-bottom p-2 lh-condensed"])[1] will select indivisual profile

Example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import time

webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service)

driver.get('https://github.com/GrahamDumpleton/wrapt/graphs/contributors')
driver.maximize_window()
time.sleep(5)

n = driver.find_element(By.XPATH,'(//*[@class="border-bottom p-2 lh-condensed"])[1]')
name= n.find_element(By.XPATH, './/a[@class="text-normal"]').text
print(name)

Output:

GrahamDumpleton
Answered By: Fazlul