![](/img/trans.png)
[英]How to extract the text H MATTHEWS from the html using Selenium and Python
[英]How to extract the text from the HTML using Selenium and Python
我有这个 HTML:
我想得到这个文本“ rataoriginal ”。 (这段文字有变化,我需要这部分代码作为文字)
我试过了
xpath = "//span[@class='_5h6Y_ _3Whw5 selectable-text invisible-space copyable-text']"
auxa = driver.find_element_by_xpath(xpath).text
print(auxa)
但它的打印结果与 print("\n") 相同。 我暂时不想使用 beaultifulsoup。
这个 HTML 来自“https://web.whatsapp.com”
//*[contains(text(),"rataoriginal")] 请使用这个 xpath
WebElement是一个动态元素,因此要打印您必须为visibility_of_element_located()
诱导WebDriverWait的值,并且您可以使用以下任一Locator Strategies :
使用CSS_SELECTOR
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.selectable-text.invisible-space.copyable-text[dir='auto']"))).text)
使用XPATH
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, '') and contains(@class, 'invisible-space')][contains(@class, '') and @dir='auto']"))).text)
注意:您必须添加以下导入:
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.