1. 主要的定位方式:xpath,cssSelector
2. web常见操作:selenium的点击与输入
#selenium 输入send_keys
self.driver=find_element(By.ID,'kw').send_keys("输入的内容")
#selenium 点击click
self.driver=find_element(By.ID,'su').click()
3. xpath定位-XML Path Language,用于解析html与xml,注意/与//的区别,注意各层级之间的嵌套关系即可
优点:适用性强,可用于selenium和appium
缺点:从头到尾遍历,比较慢,比css selector慢
练习,Google浏览器F12,Console框练习定位元素,xpath在console中的语句为$x('xpath路径')
多个元素可以使用索引或last()进行定位
4. cssSelector
适用:selenium,但是原生的appium不支持原生的cssSelector,app嵌套网页之后才可以使用cssSelector进行定位
特点:使用样式进行定位,比较快,(不同的属性用不同的符号,通过符号来定位)
练习,Google浏览器F12,Console框练习定位元素,cssSelector在console中的语句为$('cssSelector路径')
p:nth-child(n)--p元素同一个父节点的第几个孩子,只要是父节点下面的分支就是它的孩子,所以这个n值一定要注意,另查找最后一个孩子可以用p:nth-last-child(n)
其中div p等同于xpath中的//,div>p等同于xpath中的/
5. selenium+xpath/cssSelector
#selenium+xpath,打开百度页面,在搜索框中输入’测试开发‘并点击搜索按钮
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class TestCase1:
def setup_method(self, method):
self.driver = webdriver.Chrome()
self.driver.get("https://www.baidu.com")
self.driver.implicitly_wait(5)
def teardown_method(self, method):
self.driver.quit()
def test_01(self):
#xpath
#self.driver.find_element(By.XPATH,'//*[@id="kw"]').send_keys("测试开发")
#self.driver.find_element(By.XPATH, '//*[@id="su"]').click()
#cssselector
self.driver.find_element(By.CSS_SELECTOR, '#kw').send_keys("测试开发")
self.driver.find_element(By.CSS_SELECTOR, '#su').click()
注意:By.ID,By.NAME,By.LINK_TEXT底层都是用的cssSelector