zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Java [Leetcode 190]Reverse Bits
    Java [Leetcode 88]CMerge Sorted Array
    Java [Leetcode 160]Intersection of Two Linked Lists
    Java [Leetcode 111]Minimum Depth of Binary Tree
    Java [Leetcode 225]Implement Stack using Queues
    D365 FO 视图Computed字段
    D365 FO最佳实践BP(五)-Display 方法缓存
    D365 FO最佳实践BP(四)-EDT未迁移
    How to change comment
    How to force to Fullscreen Form
  • 原文地址:https://www.cnblogs.com/chenxi188/p/11640729.html
Copyright © 2011-2022 走看看