首页 技术 正文
技术 2022年11月15日
0 收藏 317 点赞 3,083 浏览 1090 个字
1、sel = driver.find_elements_by_xpath('//*[@id="nr"]')
# 搜索结果显示条数
2、sel = driver.find_element_by_xpath("//*[@id='nr']"
代码每次运行到1的时候,提示:AttributeError: 'list' object has no attribute 'tag_name',一开始以为是路径错了,抓不到,换了方式还是不行。
看了别人例子,以为是单引号和双引号造成的(知道单双引号效果是一样的,但心理作祟,人家的就是正确,改),就改成和人家例子一样的,还是报错。
再瞅,发现哪里不一样了,想打自己一顿了,发现element多了一个s,就使用2方式,对。
查找原因,函数解释如下:
def find_element_by_xpath(self, xpath):
"""
Finds an element by xpath.
:Args: - xpath - The xpath locator of the element to find.
:Returns: - WebElement - the element if it was found
:Raises: - NoSuchElementException - if the element wasn't found
:Usage: element = driver.find_element_by_xpath('//div/td[1]')
"""
return self.find_element(by=By.XPATH, value=xpath)def find_elements_by_xpath(self, xpath):
"""
Finds multiple elements by xpath.
:Args: - xpath - The xpath locator of the elements to be found.
:Returns: - list of WebElement - a list with elements if any was found. An empty list if not
:Usage: elements = driver.find_elements_by_xpath("//div[contains(@class, 'foo')]")
"""
return self.find_elements(by=By.XPATH, value=xpath)

find_element()只会查找页面符合条件的第一个节点,并返回;但是定位不到元素则会报错。

find_elements()查找多个元素并且返回一个列表,列表里的元素全是WebElement节点对象;当定位不到元素时不会报错,会返回一个空列表。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295