–
web自动化测试元素定位方法
使用WebDriver时要学习的最基本技术之一是如何在页面上查找元素。WebDriver提供了许多内置的选择器类型,其中包括通过ID、NAME、CLASS_NAME、TAG_NAME等属性查找元素的方法
WebDriver中八种不同的元素定位方法:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Edge(executable_path=r'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe')
driver.get('https://selenium.dev')
# 查找其ID属性与搜索值匹配的元素
driver.find_element_by_id('')
# 找到其NAME属性与搜索值匹配的元素
driver.find_element_by_name('')
# 查找标签名称与搜索值匹配的元素
driver.find_element_by_tag_name('')
# 查找可见文本与搜索值匹配的元素
driver.find_element_by_link_text('')
# 查找其类名包含搜索值的元素(不允许使用复合类名)
driver.find_element_by_class_name('')
# 查找可见文本包含搜索值的锚元素。如果多个元素匹配,则只会选择第一个。
driver.find_element_by_partial_link_text('')
# 找到与XPath表达式匹配的元素
driver.find_element_by_xpath('')
# 找到与CSS选择器匹配的元素
driver.find_element_by_css_selector('')
其次还有复数的定位方法:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Edge(executable_path=r'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe')
driver.get('https://selenium.dev')
driver.find_elements_by_id('')
driver.find_elements_by_name('')
driver.find_elements_by_tag_name('')
driver.find_elements_by_link_text('')
driver.find_elements_by_class_name('')
driver.find_elements_by_partial_link_text('')
driver.find_elements_by_xpath('')
driver.find_elements_by_css_selector('')
建议使定位器尽可能紧凑和可读。不然WebDriver会遍历整个DOM结构,所以尽可能缩小搜索范围,元素范围越小越好。
元素定位方法也可以这样写:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Edge(executable_path=r'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe')
driver.get('https://selenium.dev')
driver.find_element(By.CSS_SELECTOR, 'values')
这样写是可以的,但是会影响性能问题,也就是速度会慢,因为它需要向浏览器发出两个单独的命令。首先在DOM中搜索ID为“ values”的元素,然后在狭窄的上下文中搜索“values”,所以代码多时运行起来会很慢。
所以为了稍微提高性能,推荐使用更具体的定位器,优先使用CSS选择器更佳:
driver.find_element_by_css_selector('values')
WebDriver API提供了内置方法来查找基于不同属性的WebElement,例如ID,Name,Class,XPath,CSS Selectors,链接Text等。
driver.find_element(By.CSS_SELECTOR, '[type="password"]').send_keys('输入值')
driver.find_element(By.CSS_SELECTOR, '[type="password"]').text()
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').text
print(value)
driver.find_element(By.CSS_SELECTOR, '[type="password"]').clear()
aa = driver.find_element(By.CSS_SELECTOR, '[type="password"]')
print(aa)
print(aa.id)
F:\virtualEnvironment\venv\Scripts\python.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py
<selenium.webdriver.remote.webelement.WebElement (session="5b1476807d647d5446c1d5881771ae7c", element="425e77b4-1f1c-431c-af56-48048ac4250a")>
425e77b4-1f1c-431c-af56-48048ac4250a
Process finished with exit code 0
driver.find_element(By.CSS_SELECTOR, '#loginBtn').click()
driver.find_element(By.CSS_SELECTOR, '#loginBtn').submit()
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').get_attribute('outerHTML')
print(value)
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').get_property('textContent')
print(value)
F:\virtualEnvironment\venv\Scripts\python.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py
账号登录
扫码登录
Process finished with exit code 0
F:\virtualEnvironment\venv\Scripts\python.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py
<li class="tab-li default-li active">账号登录</li>
<li class="tab-li code-li">扫码登录</li>
Process finished with exit code 0
F:\virtualEnvironment\venv\Scripts\python.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py
<ul class="tab-wrapper clearfix">
<li class="tab-li default-li active">账号登录</li>
<li class="tab-li code-li">扫码登录</li>
</ul>
Process finished with exit code 0
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').is_enabled()
print(value)
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').is_displayed()
print(value)
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').is_selected()
print(value)
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').tag_name
print(value)
如果使用了括号就会报: TypeError: ‘str’ object is not callable
F:\virtualEnvironment\venv\Scripts\python.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py
Traceback (most recent call last):
File "F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py", line 44, in <module>
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').tag_name()
TypeError: 'str' object is not callable
Process finished with exit code 1
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').rect
print(value)
如果使用了括号就会报: TypeError: ‘dict’ object is not callable
F:\virtualEnvironment\venv\Scripts\python.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py
Traceback (most recent call last):
File "F:/git/AuomationTest/AuomationTestProject/webTestAuomation/auomation_script.py", line 48, in <module>
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').rect()
TypeError: 'dict' object is not callable
Process finished with exit code 1
value = driver.find_element(By.CSS_SELECTOR, '[class="tab-wrapper clearfix"]').value_of_css_property('color')
print(value)
以上都是一些常用的方法,小小总结可能会解决你的问题,也可能解决不了你的问题,但还是希望对您有所帮助,感谢阅读,如有疑义,欢迎来扰!未完待续…
一直都在努力变好中,希望您也是,加油!