1 测试?and 自动化测试?
4 元素定位一 (id, name)
4.1 八大元素定位
4.2 id 元素定位
driver.find_element(By.ID, "value")
实践
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("selenium")
driver.find_element(By.ID, "su").click()
sleep(10)
driver.quit()
4.3 name 元素定位
element = driver.find_element(By.NAME, "value")
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.NAME, "wd").send_keys("selenium")
driver.find_element(By.ID, "su").click()
sleep(10)
driver.quit()
4.4 class_name 元素定位
element = driver.find_element(By.CLASS_NAME, "value")
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.CLASS_NAME, "s_ipt").send_keys("selenium")
driver.find_element(By.ID, "su").click()
sleep(10)
driver.quit()
5 元素定位二 (tag, link)
5.1 tag_name 元素定位
element = driver.find_element(By.TAG_NAME, "value")
实践
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.taobao.com")
driver.find_element(By.TAG_NAME, "input").send_keys("键盘")
sleep(10)
driver.quit()
5.2 link_text 元素定位
element = driver.find_element(By.LINK_TEXT, "value")
实践
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.LINK_TEXT, "地图").click()
sleep(10)
driver.quit()
5.3 partial_link_text 元素定位
element = driver.find_element(By.PARTIAL_LINK_TEXT, "value")
实践
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("热点新闻")
driver.find_element(By.ID, "su").click()
sleep(3)
driver.find_element(By.PARTIAL_LINK_TEXT, "看看头条").click()
sleep(10)
driver.quit()
6 元素定位三 (xpath)
6.1 xpath 元素定位
driver.find_element(By.XPATH, "value")
代码实现
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("file:///E:/WHT/Project/test-tutorial/test-file/%E6%B5%8B%E8%AF%95.html")
driver.find_element(By.XPATH, "html/body/div/form/fieldset/table/tbody/tr/td/input").send_keys("admin")
driver.find_element(By.XPATH, "//textarea").send_keys("哈哈哈")
|