How to get text from textnodes seperated by whitespace using Selenium and Python

♀尐吖头ヾ 提交于 2021-02-05 07:44:49

问题


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.


回答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:

  • How to retrieve partial text from a text node using Selenium and Python


来源:https://stackoverflow.com/questions/63151864/how-to-get-text-from-textnodes-seperated-by-whitespace-using-selenium-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!