1

I am on this page:

https://fantasy.premierleague.com/statistics

When you click on any "i" icon next to a player, a popup window appears. Then, i want to get the surname of the player. This is how "inspect element" looks like ("whitespace" actually appears within a box):

<h2 class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ">
 Kevin
 whitespace
 De Bruyne

What i want to do is to take the text that appears after the whitespace. I can get the full text (ie both name and surname) using this:

player_full_name = driver.find_element_by_xpath('//*[@class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ"]').text

but how can i get the surname only (ie what appears after the whitespace)? Note that for other players it could have been like this:

<h2 class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ">
 Gabriel Fernando
 whitespace
 de Jesus

or like this:

<h2 class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ">
 Dean
 whitespace
 Henderson

ie splitting the text and taking the last one or two elements will not work.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
cmarios
  • 123
  • 9
  • Add your code trials to reproduce the case. – Dev Jul 29 '20 at 11:38
  • .split on the whitespace chars - '\n' or whatever? Element [-1] should be the surname. – Sean C Jul 29 '20 at 12:00
  • Why don't you use the following XPath on the same page to get the player names ? `//div[starts-with(@class,"ElementInTable__Name")]` ? Because of players like `Gabriel Fernando de Jesus` where the `de` is missing ? – E.Wiest Jul 30 '20 at 03:38
  • Many thanks to all - @E.Wiest: yes, this is a solution, but i still wanted to know how to do it. – cmarios Jul 30 '20 at 09:45

1 Answers1

1

The surname of the player is the second or last text node within it's parent WebElement. So extract the surname e.g. De Bruyne from Kevin De Bruyne you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR, childNodes and strip():

    driver.get("https://fantasy.premierleague.com/statistics")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table//tbody/tr/td/button"))).click()
    print( driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2.ElementDialog__ElementHeading-gmefnd-2")))).strip())
    
  • Console Output:

    De Bruyne
    
  • Using CSS_SELECTOR, childNodes and splitlines():

    driver.get("https://fantasy.premierleague.com/statistics")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table//tbody/tr/td/button"))).click()
    print( driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2.ElementDialog__ElementHeading-gmefnd-2")))).splitlines())
    
  • Console Output:

    ['De Bruyne']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217