一、启动浏览器
1、导入webdriver
from selenium import webdriver
2、启动谷歌、火狐、IE等浏览器
driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver = webdriver.Edge()
注:在python下引入相关驱动包
二、在浏览器中打开链接
import time
from selenium.webdriver.support import expected_conditions as EC
driver.get("访问的页面地址链接")
time.sleep(5)
print(EC.title_contains('注册'))
注: EC.title_is表示一样的title ;EC.title_contains表示包含title就可以
三、在浏览器上输入内容
1、find_element_by_id 获取ID
driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("张三")
2、find_element_by_name 获取name
driver.find_element_by_name("phone").send_keys("13222222222")
3、find_element_by_class_name 获取className
#code表示:获取父级下的className
passwordWrapper = driver.find_element_by_id("TANGRAM__PSP_4__passwordWrapper")
password = passwordWrapper.find_element_by_class_name("pass-text-input")[1] #根据index获取准确信息
password = passwordWrapper.find_element_by_class_name("pass-text-input")
password.send_keys("123456")
4、find_element_by_xpath 获取xpath
driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_4__verifyCode']").send_keys("1234")
四、visibility_of_element_located 和 presence_of_all_elements_located 区别
passwordWrapper = driver.find_element_by_id("TANGRAM__PSP_4__passwordWrapper")
EC.visibility_of_element_located #是否可见 当我们需要找到元素,并且该元素也可见
EC.presence_of_all_elements_located #当我们不关心元素是否可见,只关心元素是否存在在页面中
五、判断元素是否存在其中
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
locator = (By.ID,"TANGRAM__PSP_4__passwordWrapper")
WebDriverWait(driver,5).until(EC.visibility_of_element_located(locator))
driver.close()
六、获取提示字和输入的值
username=driver.find_element_by_id("TANGRAM__PSP_4__userName")
print(username.get_attribute("placeholder"))
username.send_keys("zhangsan")
print(username.get_attribute('value'))
七、随机生成邮箱
import random
for i in range(4):
userMail = ''.join(random.sample('12345678abc', 5))+'@qq.com'
print(userMail)
driver.close()
八、maximize_window()最大化浏览器和刷新当前页面refresh()
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
driver.maximize_window()#最大化浏览器
driver.refresh()
print(u'当前title为:',driver.title)
driver.quit()
九、图片解析和图片文字识别
(踩坑收藏有)
十、多浏览器跑case
1、if,else选择不同的浏览器跑case
2、多线程跑case