简体   繁体   English

如何使用 Selenium 和 Python 从由空格分隔的文本节点获取文本

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

I am on this page:我在这个页面上:

https://fantasy.premierleague.com/statistics https://fantasy.premierleague.com/statistics

When you click on any "i" icon next to a player, a popup window appears.当您单击播放器旁边的任何“i”图标时,会出现一个弹出窗口 window。 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.即拆分文本并获取最后一个或两个元素将不起作用。

The surname of the player is the second or last text node within it's parent WebElement .玩家的姓氏是其父WebElement中的第二个或最后一个文本节点。 So extract the surname eg De Bruyne from Kevin De Bruyne you can use either of the following Locator Strategies :因此,从Kevin De Bruyne中提取姓氏,例如De Bruyne ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR , childNodes and strip() :使用CSS_SELECTORchildNodesstrip()

     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:控制台 Output:

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

     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:控制台 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:您可以在以下位置找到一些相关的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium 和 python 从网站获取工具提示文本,其中文本来自 javascript - How can I get the tooltip text from a website using selenium and python where the text comes from a javascript 输入后如何更改textNodes中的所有文本 - How to change all text in textNodes after entering 如何从由分隔的元素中提取部分文本 <br> 通过硒标记? - How to extract partial text from an element seperated by <br> tag through selenium? 如何使用JavaScript将文本节点中的纯文本图像URL替换为img元素? - How do I replace plain text image URLs in textnodes with img elements using JavaScript? 如何使用 Python 驱动程序在 Selenium 和 web 驱动程序中获取部分文本 - How to get a part of text in Selenium and web driver using Python 如何使用硒Python在画布后面获取文本 - How to get text behind canvas using selenium python 如何通过Selenium和Python从html标签跨度获取文本 - How to get text from html tag span through Selenium and Python 如何使用 Python Selenium 从 angular 表单中提取输入文本? - How to extract the input text from angular form using Python Selenium? 如何从我们使用selenium输入的textarea获取文本 - How to get text from a textarea we entered using selenium 如何使用Selenium从JavaScript窗口获取文本? - How to get a text from a JavaScript window using Selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM