本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
阿里云开发者社区用户服务协议
》和
《
阿里云开发者社区知识产权保护指引
》。如果您发现本社区中有涉嫌抄袭的内容,填写
侵权投诉表单
进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
4、通过空格键执行页面滚动操作
1.摁空格键
2.报错:TypeError: list indices must be integers or slices, not WebElement
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:
# 将页面向下拉取400像素
print(f"将页面向下拉取{int(index/5+1)*400}像素")
self.driver.execute_script(f"window.scrollTo(0,{int(index/5+1)*420});")
time.sleep(3)
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()
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()
#获取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)