自动化软件测试 -- Selenium(二)

一、浏览器操作

设置浏览器

from selenium import webdriver
import time
#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "http://www.baidu.com/"

driver.get(url)

#最大化浏览器
#driver.maxsize_window()

#设置浏览器 800 * 600
time.sleep(1)
driver.set_window_size(800,600)
time.sleep(2)
driver.set_window_position(200,200)
time.sleep(5)

#回收资源
driver.quit()

前进、后退、刷新

from selenium import webdriver
import time
#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "http://www.baidu.com/"

driver.get(url)

driver.find_element_by_id("kw").send_keys("python")
driver.find_element_by_id("su").click()
time.sleep(2)

#浏览器后退
driver.back()
time.sleep(2)

#浏览器前进
driver.forward()
time.sleep(2)

#刷新浏览器
driver.refresh()
time.sleep(2)

driver.back()
driver.find_element_by_link_text("hao123").click()
time.sleep(2)

driver.close()
time.sleep(5)

#回收资源
driver.quit()

获取页面title,获取当前页面的url

from selenium import webdriver
import time
#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "http://www.baidu.com/"

driver.get(url)

#最大化浏览器
driver.maximize_window()

print(driver.title)
print(driver.current_url)
driver.find_element_by_id("kw").send_keys("python")
driver.find_element_by_id("su").click()

time.sleep(2)
print(driver.title)
print(driver.current_url)

#浏览器后退
driver.back()
print(driver.title)
print(driver.current_url)

#浏览器前进
driver.forward()
print(driver.title)
print(driver.current_url)
time.sleep(2)

driver.close()
time.sleep(5)

#回收资源
driver.quit()

二、页面等待 

from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "http://www.baidu.com/"

driver.get(url)

#输入要搜索的内容
driver.find_element_by_id("kw").send_keys("美女")
driver.find_element_by_id("su").click()
#点集一个搜索到的条目
#代码的执行速度远远快于页面的加载速度的,在翻页或者在加载新的页面的时候,需要进行页面等待
#等待的方式有三种:强制等待、显式等待、隐式等待

# #强制等待
time.sleep(1)
driver.find_element_by_id("1").click()

#显式等待
#等待某个元素加载完成,每隔0.5秒去加载一次,最多等待5秒时间
WebDriverWait(driver,5).until(EC.presence_of_all_elements_located(By.ID,"1"))
driver.find_element_by_id("1").click()

#隐式等待
driver.implicitly_wait(5)
driver.find_element_by_id("1").click()

time.sleep(5)

#回收资源
driver.quit()

三、鼠标操作

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains

#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "http://www.baidu.com/"

driver.get(url)
action = ActionChains(driver)
#点击右键
action.context_click(driver.find_element_by_id("su"))

#悬停
action.move_to_element(driver.find_element_by_class_name("soutu-btn"))

#拖拽
action.drag_and_drop(driver.find_element_by_id("div1"),driver.find_element_by_id("div2"))

#事件操作必须要执行perform
action.perform()

time.sleep(5)


#回收资源
driver.quit()

四、键盘操作

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "http://www.baidu.com/"

driver.get(url)

el = driver.find_element_by_id("kw")

#输入python
el.send_keys("python")
time.sleep(2)
#全选
el.send_keys(Keys.CONTROL,"a")
time.sleep(2)
#删除
el.send_keys(Keys.BACK_SPACE)
time.sleep(2)
#再输入郁金香
el.send_keys("郁金香")
time.sleep(2)
#全选
el.send_keys(Keys.CONTROL,"a")
time.sleep(2)
#复制
el.send_keys(Keys.CONTROL,"c")
time.sleep(2)
#粘贴
el.send_keys(Keys.CONTROL,"v")

time.sleep(5)

#回收资源
driver.quit()

五、下拉框操作

from selenium import webdriver
import time
from selenium.webdriver.support.select import Select


#准备一个网址
url = "file://D:/ship/kc/11.selenium/%E8%B5%B4%E6%96%99/%E6%B3%AB%E5%86%BCA.html"

driver.get(url)

select = Select(driver.find_element_by_id("selectA"))
time.sleep(2)
select.select_by_index(3)
time.sleep(2)
select.select_by_value("bj")
time.sleep(2)
select.select_by_visible_text("A广州")

time.sleep(5)

#回收资源
driver.quit()

六、页面滚动

from selenium import webdriver
import time
#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "https://www.douyu.com/directory/all"

driver.get(url)
driver.maximize_window()
time.sleep(3)

js_str = "window.scrollTo(0,10000)"
driver.execute_script(js_str)

#driver.find_element_by_xpath("//*[text()='下一页']").click()


time.sleep(5)

#回收资源
driver.quit()

七、警告框处理

from selenium import webdriver
import time
#webdriver 获取浏览器的对象
driver = webdriver.Chrome("chromedriver.exe")
#准备一个网址
url = "file://D:/ship/kc/11.selenium/%E8%B5%B4%E6%96%99/%E6%B3%AB%E5%86%BCA.html"

driver.get(url)
time.sleep(2)

driver.find_element_by_id("alerta").click()

#遇到警告框,先切换到警告框,然后再操作
driver.switch_to.alert

print(alert.text)
alert.dismiss()
time.sleep(5)

#回收资源
driver.quit()

你可能感兴趣的:(自动化,selenium,python)