我在这一页:
https://fantasy.premierleague.com/statistics
当您单击播放器旁边的任何“i”图标时,会出现一个弹出窗口。然后,我想知道玩家的姓氏。这就是“inspect元素”的外观(“空白”实际上出现在一个框中):
<h2 class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ">
Kevin
whitespace
De Bruyne
我想做的是获取出现在空白后面的文本。我可以通过以下方式获取全文(即姓名和姓氏):
player_full_name = driver.find_element_by_xpath('//*[@class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ"]').text
但我怎样才能只知道姓氏(空格后面是什么)?请注意,对于其他玩家来说,可能是这样的:
<h2 class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ">
Gabriel Fernando
whitespace
de Jesus
或者像这样:
<h2 class="ElementDialog__ElementHeading-gmefnd-2 ijAScJ">
Dean
whitespace
Henderson
即拆分文本并提取最后一两个元素将不起作用
玩家的姓氏是其父节点WebElement中的第二个或最后一个文本节点。因此,从Kevin De Bruyne中提取姓氏,例如,De Bruyne。您可以使用以下任意一种Locator Strategies:
使用
CSS_SELECTOR
、childNodes和strip()
:控制台输出:
使用
CSS_SELECTOR
、childNodes和splitlines()
:控制台输出:
注意:您必须添加以下导入:
参考资料
您可以在以下内容中找到一些相关的详细讨论:
相关问题 更多 >
编程相关推荐