xpath中常用的方法

舊人學習筆記發表於2020-10-16

獲取屬性的值和標籤中的文字

有時候不能直接定位到標籤的屬性,需要首先定位到webelement,之後get到屬性

try:
   temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/@href')
   temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a').get_attribute('href')
except Exception as e:
    print(e)
try:
    temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a/@href')
    temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a').get_attribute('href')
except Exception as e:
    print(e)
try:
    temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a/@title')
    temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a').get_attribute('title')
except Exception as e:
    print(e)
try:
    temp['user'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/text()')
    temp['user'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a').text
except Exception as e:
    print(e)

相關文章