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

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

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:

# Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class")

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

#获取元素标签的内容:
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执行页面滚动条操作

#滚动屏幕元素可见
# 将页面向下拉取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键点击页面未展示元素

用法实例

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.摁空格键

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

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

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

#获取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)

上面运行是会报错:

修改为如下即可:

#获取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)

欢迎关注个人公众号
梦无矶的测试开发之路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值