1 Selenium Python 基础
生活随笔
收集整理的這篇文章主要介紹了
1 Selenium Python 基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 環境搭建
- setuptools:setuptools是一組Python的distutilsde工具的增強工具,可以讓程序員更方便的創建和發布Python包
? ? ? ?下載地址:https://pypi.python.org/pypi/setuptools#downloads
? ? ? ?安裝:cd setuptools目錄,執行 python setup.py install
- pip:pip是python的安裝包管理工具
? ? ? ?下載地址:https://pypi.python.org/pypi/pip#downloads
? ? ? ?安裝:cd pip目錄,執行python setup.py install,安裝完畢需要設置環境變量path,C:\Python27\Scripts
- selenium:UI自動化測試開源工具
? ? ? ?最新版安裝:pip install selenium
? ? ? ?指定版安裝:pip install selenium==2.48.0
? ? ? ?查看版本:pip show selenium
?2 第一個程序
?那百度的搜索當作第一個例子
1 from selenium import webdriver 2 3 driver = webdriver.Firefox() 4 driver.maximize_window() 5 driver.get("https://www.baidu.com/") 6 7 #搜索 8 driver.find_element_by_xpath("//input[@id='kw']").send_keys("java") 9 driver.find_element_by_xpath("//input[@id='su']").click() View Code?3 對象識別
3.1瀏覽器操作
1 from selenium import webdriver 2 3 driver = webdriver.Firefox(); #啟動瀏覽器 4 driver.maximize_window(); #瀏覽器最大化 5 dirver.get("https://www.baidu.com"); #瀏覽器導航 6 driver.back(); #瀏覽器后退 7 driver.forward(); #瀏覽器前進 8 driver.refresh(); #瀏覽器刷新 View Code3.2一般操作
1 from selenium import webdriver 2 3 #鏈接 4 linkLogin = driver.find_element_by_xpath("//div[@id='u1']/a[text()='登錄']"); 5 linkLogin.click(); 6 7 #輸入框 8 textUsername = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_8__userName']"); 9 textUsername.clear(); 10 textUsername.sendKeys(“測試開發"); 11 12 #按鈕 13 WebElement buttonLogin = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_8__submit']"); 14 buttonLogin.click(); 15 16 #懸停 17 my = driver.find_element_by_xpath("//a[@id='s_username_top']/span"); 18 action = ActionChains(driver); 19 action.move_to_element(my).perform(); 20 21 #單選 22 #方法1 23 radiosGender = driver.find_element_by_xpath("//input[@id='passport-sex-2']"); 24 radiosGender.click(); 25 #方法2 – 從0開始 26 radiosGender = driver.find_elements_by_xpath("//input[@name='passport_sex']"); 27 radiosGender.pop(0).click(); 28 29 #多選框 30 #方法1 31 xg = driver.find_element_by_xpath("//input[@id='passport-character-1']"); 32 xg.click(); 33 #方法2 – 從0開始 34 xg = driver.find_elements_by_xpath("//input[@name='passport_character']"); 35 xg.pop(0).click(); 36 37 #下拉 38 from selenium.webdriver.support.ui import Select 39 40 time =driver.find_element_by_xpath("//div[@id='main-wraper']//select[1]"); 41 select = Select(time); 42 select.select_by_visible_text("三個月內"); 43 44 #JS 45 js = "alert('你好');" 46 driver.execute_script(js); 47 alert =driver.switch_to_alert(); 48 alert.accept(); 49 50 #上傳文件 51 file = driver.find_element_by_xpath("//div[@id='uploadfile']/input"); 52 file.sendKeys("C:\\test.txt"); 53 54 #iframe切換 55 baidu = driver.find_element_by_xpath("//iframe[@id='baidu']"); 56 driver.switch_to_frame(baidu); #切換到iframe 57 …… 58 …… 59 driver.switch_to.parent_frame(); #切換回原窗口 60 61 #彈出窗口 62 link = driver.find_element_by_xpath("//div[@id='alert']/input"); 63 link.click(); 64 alert =driver.switch_to_alert(); 65 alert.accept(); 66 67 #窗口切換 68 def switchToWindow(title): 69 currentHandle = driver.current_window_handle; 70 handles = driver.window_handles; 71 72 for handle in handles: 73 if handle == currentHandle: 74 continue; 75 else: 76 driver.switch_to_window(handle); 77 if title in driver.title: 78 break; 79 else: 80 continue; 81 switchToWindow("帳號設置"); View Code3.3 鼠標操作
1 from selenium.webdriver.common.action_chains import ActionChains 2 3 #懸停 4 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 5 action = ActionChains(driver); 6 action.move_to_element(element).perform(); 7 #右擊 8 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 9 action = ActionChains(driver); 10 action.context_click(element).perform(); 11 #雙擊 12 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 13 action = ActionChains(driver); 14 action.double_click(element).perform(); 15 #拖拽 16 source = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 17 target = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 18 action = ActionChains(driver); 19 action.drag_and_drop(source, target).perform(); View Code3.4 鍵盤操作
1 from selenium.webdriver.common.keys import Keys 2 3 #全選 4 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 5 element.send_keys(Keys.CONTROL,"a"); 6 #復制 7 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 8 element.send_keys(Keys.CONTROL,“c"); 9 #粘貼 10 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 11 element.send_keys(Keys.CONTROL,“v"); 12 #回車 13 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']"); 14 element.send_keys(Keys.ENTER); View Code轉載于:https://www.cnblogs.com/lizitest/p/6616723.html
總結
以上是生活随笔為你收集整理的1 Selenium Python 基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言程序设计第四次作业——顺序结构
- 下一篇: python Unable to fin