如何使用Selenium和Python从文本节点检索部分文本 [英] How to retrieve partial text from a text node using Selenium and Python

查看:118
本文介绍了如何使用Selenium和Python从文本节点检索部分文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望不使用.split()或索引切片来获取"text ..."

I want to get only " text ... " not using .split() or index slicing

HTML:

<a class="call_recipe" href="/recipes/2913">
      " text ... "
      <strong> something~ </strong>
    </a>

HTML快照:

推荐答案

要打印文本... ,您必须诱使定位器策略:

To print text ... you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • 使用CSS_SELECTOR childNodes strip():

print(driver.execute_script('return arguments[0].firstChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.call_recipe[href^='/recipes']")))).strip())

  • 使用XPATHget_attribute()splitlines():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='call_recipe' and starts-with(@href, '/recipes')]"))).get_attribute("innerHTML").splitlines()[1])
    

  • 注意:您必须添加以下导入:

  • 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:

    这篇关于如何使用Selenium和Python从文本节点检索部分文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆