1

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.

2 Answers 2

1

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(.,'--')]

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! @Prophet. I also want to collect the addition and deletion number of Graham. So I try: addition = header.find_element(By.XPATH, ".//a[contains(.,' ++')]"); deletion = header.find_element(By.XPATH, ".//a[contains(.,' --')]"), but it cannot find any element. Could you please help me figure out where the problem could be? Thanks again! The Xpath of addition and deletion are: //*[@id="contributors"]/ol/li/span/h3/span[2]/span/div/span[1]; //*[@id="contributors"]/ol/li/span/h3/span[2]/span/div/span[2]
Great thanks, @Prophet. It works. But it provides information like this: 495 commits 31,627 ++ 9,898 -- My code is: addition = header.find_element(By.XPATH, ".//span[contains(@class,'cmeta')][contains(.,'++')]"). How should I improve the code to let it show addition only?
This can't be done with selenium since what you getting there is the content of that element. Now you can replace the ++ and -- with empty string and trim spaces before and after the text
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.