打印索引元素的文本属性:
def print_list(coordinate_list):
print('当前项目地块数:', len(coordinate_list))
for i in range(0, len(coordinate_list)):
print(i)
print(coordinate_list[i].text)
定义一个根据元素文本获取元素的函数:
def getElementByText(coordinate_list, myvalue):
for i in range(0, len(coordinate_list)):
if coordinate_list[i].text == myvalue:
return coordinate_list[i]
else:
print("Failure!")
获取元素value值
driver.find_element(By.ID, "kw").send_keys("ROOT")
user_name = driver.find_element(By.ID, "kw")
text_user_name = user_name.get_attribute("value")
print(text_user_name)
看一下By支持的方法
class By:
"""Set of supported locator strategies."""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
示例
element = driver.find_element(By.TAG_NAME,"html")
# 替换为您要检查的标签
attribute_value = element.get_attribute("data-theme") # 替换为您要获取的属性名称
# 打印属性值
print("html标签的属性值:", attribute_value)
CLASS_NAME、NAME、ID同理,以www.baidu.com为例文章来源:https://www.toymoban.com/news/detail-721928.html
文章来源地址https://www.toymoban.com/news/detail-721928.html
到了这里,关于Selenium的find_element()与find_elements()和By的几种方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!