selumium 中 xpath獲取文本、屬性正確寫法


報錯“The result of the xpath expression is: [object Attr]. It should be an element”

yutube爬蟲動態加載,需要用到selenium-webdriver,使用過程中,首先使用

find_elements_by_xpath進行批量標簽的定位選取,之后

使用find_element_by_xpath精細篩選選標簽的時候出現上面錯誤提示,

原因是這個webdriver的定位方法和瀏覽器xpath不一樣,不能直接定位到標簽的屬性

需要首先定位到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').get_attribute('href')
            except Exception as e:
                print(e)
            try:
                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').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
            except Exception as e:

錯誤:

try:
                temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/@href')
            except Exception as e:
                print(e)
            try:
                temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a/@href')
            except Exception as e:
                print(e)
            try:
                temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a/@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()')
            except Exception as e:
                print(e)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM