【问题标题】:Skip extracting value if text node not found如果未找到文本节点,则跳过提取值
【发布时间】:2020-08-05 15:46:04
【问题描述】:

我正在从this page 那里获取所有开盘赔率 但我想跳过所有取消的事件(如this page)并打印如下空白结果:

try:
    xpath = '//table[starts-with(@id,"aodds")]//tr[th="Opening odds"]/following-sibling::tr/td[@class="bold"]'
except:
    print('')
    homeodd = driver.find_element_by_xpath(xpath).text

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver xpath


    【解决方案1】:

    尝试将您的代码更新为

    from selenium.common.exceptions import NoSuchElementException
    
    xpath = '//table[starts-with(@id,"aodds")]//tr[th="Opening odds"]/following-sibling::tr/td[@class="bold"]'
    
    try:
        homeodd = driver.find_element_by_xpath(xpath).text
    except NoSuchElementException:
        print('No bets found')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 2015-03-13
      • 2011-10-13
      相关资源
      最近更新 更多