selenium 实战

iframe

driver.switch_to_frame(id="xx")

多窗口

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get("http://mail.sina.com.cn/")
driver.implicitly_wait(30)
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[7]/div[1]/a[2]").click()
sleep(2)

now_handle = driver.current_window_handle
print("now_handle:%s"%now_handle)
for i in driver.window_handles:
    print("i:%s"%i)
    if i!=now_handle:
        driver.switch_to_window(i)
        sleep(2)
        driver.find_element_by_name('email').send_keys("123")
View Code

刷新
driver.refresh()

获取执行的浏览器
driver.name

获取属性
name=driver.find_element_by_id("kw")
name.get_attribute("value")

元素是否可见
name.is_displayed()

元素是否可编辑
name.is_enabled()

元素是否可选中
name.is_selected()

悬浮/鼠标右击/鼠标双击
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
name=driver.find_element_by_id("kw")
ActionChains(driver).move_to_element(name).perform()

示例2:
action_obj = ActionChains(driver)
name = driver.find_element_by_id("freename")
action_obj.context_click(name).perform()
action_obj.double_click(name).perform()
View Code


下拉框
from selenium import webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()
name = driver.find_element_by_id("kw")
select_obj = Select(name)
select_obj.select_by_index(2)
select_obj.select_by_value("name")
select_obj.select_by_visible_text('每条20个')
View Code


弹框
from selenium import webdriver

driver = webdriver.Chrome()
driver.switch_to_alert().text
driver.switch_to_alert().accept()
driver.switch_to_alert().dismiss()
driver.switch_to_alert().send_keys('xx')
View Code


 等待
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("http://mail.sina.com.cn/")
so = WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable((By.ID,'kw')))
so.click()
View Code
 
 指定元素的文本位置/判断元素是否可见
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("http://mail.sina.com.cn/")
driver.find_element_by_id("freename").send_keys(' ')
sleep(2)
driver.find_element_by_id("freepassword").send_keys(' ')
sleep(2)
driver.find_element_by_link_text('登录').click()
isTest = WebDriverWait(driver,10).until(expected_conditions.text_to_be_present_in_element((By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]'),'请输入邮箱名'))
isDisplay = WebDriverWait(driver,10).until(expected_conditions.visibility_of_element_located((By.XPATH,'请输入邮箱名')))
if isTest:
    print('ok')
View Code

 

浏览器滑动
from selenium import webdriver
from time import sleep


driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.get("https://www.cnblogs.com/zhang-dan/p/9977604.html")
down = "var q=document.documentElement.scrollTop=1000"
sleep(3)
driver.execute_script(down)
sleep(3)
top = "var q=document.documentElement.scrollTop=0"
sleep(3)
driver.execute_script(top)
sleep(3)
View Code

 

富文本处理

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(30)

def richText(data):
    "在富文本里添加内容"
    js = "document.getElementById('ueditor_0').contentWindow.document.body.innerHTML='%s'"%(data)
    driver.execute_script(js)

driver.get("http://ueditor.baidu.com/website/onlinedemo.html")
richText("haha")
sleep(3)
View Code

 

取消只读属性

def cancellOnlyRead(data):
    " 取消只读属性,并添加内容 "
    js = "$(\"input[placeholder='start_tome']\").removeAttr('readonly'); $(\"input[placeholder='start_tome']\").attr('value','%s')"%data
    driver.execute_script(js)
View Code

 

屏幕截图

from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(30)

driver.get("http://www.baidu.com")
driver.save_screenshot('baidu.png')
driver.get_screenshot_as_file('baidu2.png')
View Code

 

 
posted @ 2019-08-06 00:15  东方不败--Never  阅读(181)  评论(0编辑  收藏  举报