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)
  • 相关阅读:
    OSPF 开放最短路径优先协议
    RIP 路由算法
    原创 记一个上门洗车服务范围的需求实现
    转载 一位资深程序员大牛给予Java学习者的学习路线建议
    原创 解决异步调用实时跳转
    FIFO队列 ADT接口 数组实现
    FIFO队列 ADT接口 链表实现
    约瑟夫问题 链表实现
    合并-查找接口实现
    快速查找 快速合并 加权快速合并 路径等分加权快速合并 算法
  • 原文地址:https://www.cnblogs.com/chenxi188/p/11640729.html
Copyright © 2011-2022 走看看