前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >web自动化之selenium的特殊用法(一)

web自动化之selenium的特殊用法(一)

作者头像
梦无矶小仔
发布2022-06-30 18:51:18
8050
发布2022-06-30 18:51:18
举报
文章被收录于专栏:梦无矶的测试开发之路

目录 1、get_attribute()

2、js滚动页面

3、Tab键点击页面未展示元素

4、通过空格键执行滚动滚动操作

1.摁空格键

2.报错:TypeError: list indices must be integers or slices, not WebElement

1、get_attribute()

官方文档释义

selenium.webdriver.remote.webelement — Selenium 4.1.0 documentation

get_attribute(name) → str[source]

Gets the given attribute or property of the element.

获取元素的给定属性或属性。

This method will first try to return the value of a property with the given name. If a property with that name doesn’t exist, it returns the value of the attribute with the same name. If there’s no attribute with that name, None is returned.

该方法将首先尝试返回具有给定名称的属性的值。如果具有该名称的属性不存在,则返回具有相同名称的属性的值。如果没有这个名称的属性,则返回' None '。

Values which are considered truthy, that is equals “true” or “false”, are returned as booleans. All other non-None values are returned as strings. For attributes or properties which do not exist, None is returned.

被认为为真值的值,即等于“真”或“假”的值,将作为布尔值返回。所有其他非' None '值将作为字符串返回。对于不存在的属性或属性,将返回' None '。

To obtain the exact value of the attribute or property, use get_dom_attribute() or get_property() methods respectively.

要获得属性或属性的确切值,请分别使用' get_dom_attribute() '或' get_property() '方法。

Example:

代码语言:javascript
复制
# Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class")

里面可以填所有的属性,目前我尝试过的有如下几个

代码语言:javascript
复制
#获取元素标签的内容:
get_attribute('textContent')

#获取元素内的全部HTML:
get_attribute('innerHTML')

#获取包含选中元素的HTML:
get_attribute('outerHTML')

get_attribute('class')

get_attribute('name')

get_attribute('id')

get_attribute('href')

2、js滚动页面

通过js执行页面滚动条操作

代码语言:javascript
复制
#滚动屏幕元素可见
# 将页面向下拉取400像素
print(f"将页面向下拉取{int(index/5+1)*400}像素")
self.driver.execute_script(f"window.scrollTo(0,{int(index/5+1)*420});")
time.sleep(3)

3、Tab键点击页面未展示元素

用法实例

代码语言:javascript
复制
history_element_id = "changehistory-tabpanel"
history_element = self.driver.find_element_by_id(history_element_id)
# time.sleep(0.5)
history_element.send_keys(Keys.TAB) #通过tab键来查找页面元素
#点击history的按钮,使得下面的内容显示出来
history_element.click()

4、通过空格键执行页面滚动操作

终极大法,按住下键或者摁空格键可以到达页面底部

1.摁空格键

注意:如果页面有多个滚动条,则需要鼠标左键单击对应的滚动条对应页面

直接进入页面点空格键是没有反应的,需要点击一下页面再摁空格键才有效果

代码语言:javascript
复制
from selenium.webdriver.common.action_chains import ActionChains
print(2)
print("移动鼠标点击左键  ")
ActionChains(self.driver).move_by_offset(300, 900).click().perform()
print("摁住空格键")
time.sleep(3)
actions = ActionChains(self.driver)
actions.send_keys(Keys.SPACE).perform()

2.报错:TypeError: list indices must be integers or slices, not WebElement

代码语言:javascript
复制
#获取25条数据
crashNum = self.driver.find_elements(By.CLASS_NAME,'particle-table-row')
print(len(crashNum))
for i in crashNum:
    particle_table_row = self.driver.find_elements(By.CLASS_NAME,'particle-table-row')[i]
    msg = particle_table_row.text
    print(msg)

上面运行是会报错:

修改为如下即可:

代码语言:javascript
复制
#获取25条数据
crashNumList = self.driver.find_elements(By.CLASS_NAME,'particle-table-row')
print(len(crashNumList))
for crashNumListIndex,crashNum in enumerate(crashNumList):
    particle_table_row = crashNumList[crashNumListIndex]
    msg = particle_table_row.text
    print(msg)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 梦无矶的测试开发之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、get_attribute()
  • 2、js滚动页面
  • 3、Tab键点击页面未展示元素
  • 4、通过空格键执行页面滚动操作
    • 1.摁空格键
      • 2.报错:TypeError: list indices must be integers or slices, not WebElement
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档