![](/img/trans.png)
[英]How can I get the tooltip text from a website using selenium and python where the text comes from a javascript
[英]How to get text from textnodes seperated by whitespace using Selenium and Python
我在这个页面上:
https://fantasy.premierleague.com/statistics
当您单击播放器旁边的任何“i”图标时,会出现一个弹出窗口 window。 然后,我想获得玩家的姓氏。 这就是“检查元素”的样子(“空白”实际上出现在一个框中):
<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 ,您可以使用以下任一定位器策略:
使用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())
控制台 Output:
De Bruyne
使用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())
控制台 Output:
['De Bruyne']
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些相关的详细讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.