PythonSelemium元素定位
一、html
下面的html文件是后面要使用到的form.html,页面大概如下:
<!DOCTYPE html><html>
<head>
<title>查找元素</title>
<meta charset="utf-8">
</head>
<body>
<div id="container">
<form>
user:<input type="text" name="user" id="user"><br /><br />
email:<input type="text" name="email" id="email"><br /><br />
male:<input type="radio" name="sex" id="male" checked="">
female:<input type="radio" name="sex" id="female"><br /><br />
读书:<input type="checkbox" name="hobby" id="read" checked="">
电影:<input type="checkbox" name="hobby" id="movie">
运动:<input type="checkbox" name="hobby" id="sport">
<br /><br />
语言:<select>
<option value="Python" id="python">Python</option>
<option value="Java" id="java" selected="selected">Java</option>
<option value="C++" id="cpp">C++</option>
</select>
<br /><br />
<button type="submit">提交</button>
<button type="reset">重置</button>
</form>
</div>
<div>
<a href="abc" class="aele">abc</a><br />
<a href="bbc" class="aele">bbc</a><br />
<a href="def" class="aele">def</a><br />
</div>
</body>
</html>
二、WebElement
方法属性 说明
id_
获取元素的ID
tag_name
获取元素标签名
text
获取元素的文本
click()
单击元素
submit()
提交表单
clear()
清除一个文本输入元素的文本
get_attribute()
获得元素属性值
is_selected()
判断元素是否被选中
is_enabled()
判断元素是否可用
send_keys()
在输入框里输入内容
没有括号的是属性,有括号的是方法
三、id与name属性选择
find_element_by_id(id_)find_elements_by_id(id_)
find_element_by_name(attr_name)
find_elements_by_name(attr_name)
from selenium import webdriverdriver = webdriver.Firefox()
file_path = r"file:///F: mpform.html"
driver.get(file_path)
user_ele = driver.find_element_by_id("user")
# text
print(user_ele.get_attribute("type"))
# input
print(user_ele.tag_name)
# True
print(user_ele.is_enabled())
# 将改元素输入值curitis
user_ele.send_keys("curitis")
sex_ele = driver.find_element_by_name("sex")
# <class "selenium.webdriver.firefox.webelement.FirefoxWebElement">
print(type(sex_ele))
# True
print(sex_ele.is_selected())
# male
print(sex_ele.get_attribute("id"))
sex_eles = driver.find_elements_by_name("sex")
# <class "list">
print(type(sex_eles))
# True male|True male|
for ele in sex_eles:
print(" ".join([str(sex_ele.is_selected()), str(sex_ele.get_attribute("id"))]), end="|")
print()
python_ele = driver.find_element_by_id("python")
# False
print(python_ele.is_selected())
java_ele = driver.find_element_by_id("java")
# True
print(java_ele.is_selected())
四、tag与class名称选择
find_element_by_tag_name(tag_name)find_elements_by_tag_name(tag_name)
find_element_by_class_name(class_name)
find_elements_by_class_name(class_name)
from selenium import webdriveroptions = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options)
file_path = r"file:///F: mpform.html"
driver.get(file_path)
aele = driver.find_element_by_tag_name("a")
# abc
print(aele.text)
aeles = driver.find_elements_by_tag_name("a")
# abc bbc def
for aele in aeles:
print(aele.text, end=" ")
print()
aele = driver.find_element_by_class_name("aele")
# abc
print(aele.text)
aeles = driver.find_elements_by_class_name("aele")
# abc bbc def
for aele in aeles:
print(aele.text, end=" ")
五、css选择器
find_element_by_css_selector(css_selector)find_elements_by_css_selector(css_selector)
from selenium import webdriveroptions = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options)
file_path = r"file:///F: mpform.html"
driver.get(file_path)
pytho_sele = driver.find_element_by_css_selector("#python")
# Python
print(pytho_sele.get_attribute("value"))
aele = driver.find_element_by_css_selector(".aele")
# abc
print(aele.text)
aeles = driver.find_elements_by_css_selector(".aele")
# abc bbc def
for aele in aeles:
print(aele.text, end=" ")
六、xpath
find_element_by_xpath(xpath)find_elements_by_xpath(xpath)
from selenium import webdriveroptions = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options)
file_path = r"file:///F: mpform.html"
driver.get(file_path)
# 查找id为email的input
print(driver.find_element_by_xpath("//input[@id="email"]").get_attribute("id"))
# div后的a的倒数第2个
print(driver.find_element_by_xpath("//div/a[last()-1]").text)
# div下的第一个a
print(driver.find_element_by_xpath("//div/a[position()<2]").text)
# div下a标签下有个文本包含"e"的元素
print(driver.find_element_by_xpath("//div/a[contains(text(),"e")]").text)
# div下href="bbc"的a标签
print(driver.find_element_by_xpath("//div/a[@href="bbc"]").text)
七、链接定位
find_element_by_link_text(link_text)find_elements_by_link_text(link_text)
find_element_by_partial_link_text(partial_link_text)
find_elements_by_partial_link_text(partial_link_text)
from selenium import webdriveroptions = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options)
file_path = r"file:///F: mpform.html"
driver.get(file_path)
# 必须完全匹配
print(driver.find_element_by_link_text("abc").get_attribute("href"))
# print(driver.find_element_by_link_text("a"))
# 只要标签文本中包含b就可以
btxt = driver.find_elements_by_partial_link_text("b")
for txt in btxt:
print(txt.get_attribute("href"))
以上是 PythonSelemium元素定位 的全部内容, 来源链接: utcz.com/z/510886.html