【问题标题】:How do I identify this numeric value using python selenium?如何使用 python selenium 识别这个数值?
【发布时间】:2019-09-24 15:26:26
【问题描述】:

因此,对于这些行中的每一行,都有一个“xxx Answers”元素。

如何提取它的数值,以便进行比较,例如:

如果 x > 50 那么等等等等。

【问题讨论】:

  • 请以文本格式提供示例数据。

标签: python selenium selenium-webdriver selenium-chromedriver webautomation


【解决方案1】:

这是获取值的 xpath。

//div[@class='u-text--gray-light-metadata']/a

Python 代码应如下所示(每个名称都有多个答案元素 1,因此您必须获取元素并进行迭代,但这里我将提供如何获取第一个元素的示例)

text =  driver.find_element_by_xpath("(//div[@class='u-text--gray-light-metadata']/a)[1]").text
# extract the number of answers
answers = text.split(' ')[0]

【讨论】:

    【解决方案2】:

    一个粗略的通用解决方案应该可以解决所有问题:

    import re
    import selenium
    
    driver = webdriver.Firefox()
    driver.get("http://example.com/") # your website here
    
    anchors = driver.find_elements_by_tag_name('a')
    
    matches = []
    for anchor in anchors:
        match = re.fullmatch(r'(\d+) Answers?', anchor.text)
        if match:
            matches.append(match.group(1))
    
    print(matches)
    

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多