[英]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_SELECTOR
、 childNodes和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:控制台 Output:
De Bruyne
Using CSS_SELECTOR
, childNodes and splitlines()
:使用
CSS_SELECTOR
、 childNodes和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:控制台 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
You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.