标签:send form element coding class lin 返回 xpath evel
元素定位
#coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary import time binary = FirefoxBinary(‘/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin‘) driver = webdriver.Firefox(firefox_binary=binary) driver.get(‘http://www.baidu.com‘) size = driver.find_element_by_id(‘kw‘).size print(size) text = driver.find_element_by_id(‘cp‘).text print(text) size1 = driver.find_element_by_id(‘kw‘).get_attribute(‘type‘) print(size1) size2 = driver.find_element_by_id(‘kw‘).is_displayed() print(size2) driver.find_element_by_id(‘kw‘).clear() driver.find_element_by_id(‘kw‘).send_keys(‘selenium‘) driver.find_element_by_id(‘su‘).click() time.sleep(2) driver.get(‘http://news.baidu.com‘) time.sleep(2) driver.back() time.sleep(2) driver.forward() time.sleep(2) driver.refresh()
鼠标键盘操作
#coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import time binary = FirefoxBinary(‘/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin‘) driver = webdriver.Firefox(firefox_binary=binary) driver.get(‘http://www.baidu.com‘) right_click = driver.find_element_by_id("listHolder") # 右键 ActionChains(driver).context_click(right_click).perform() # 鼠标悬停 above = driver.find_element_by_name("tj_settingicon") ActionChains(driver).move_to_element(above).perform() #双击 double_click = driver.find_element_by_id(‘xx‘) ActionChains(driver).double_click(double_click).perform() # 拖动 drag_start = driver.find_element_by_id(‘xx‘) drag_end = driver.find_element_by_id(‘cc‘) ActionChains(driver).drag_and_drop(drag_start,drag_end).perform() #输入框输入内容 driver.find_element_by_id(‘kw‘).send_keys(‘hha‘) #删除多输入的内容 driver.find_element_by_id(‘kw‘).send_keys(Keys.BACK_SPACE) #输入空格 driver.find_element_by_id(‘kw‘).send_keys(Keys.SPACE) # ctrl+a全选 driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL,‘a‘) #回车 driver.find_element_by_id(‘kw‘).send_keys(Keys.ENTER)
显式/隐式等待
#coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC binary = FirefoxBinary(‘/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin‘) driver = webdriver.Firefox(firefox_binary=binary) driver.get(‘http://www.baidu.com‘) #显式等待 element = WebDriverWait(driver,5,0.5).until(EC.presence_of_all_elements_located((By.ID,‘kw‘))) element.send_keys(‘ss‘) #隐式等待 from selenium.common.exceptions import NoSuchElementException driver.implicitly_wait(10) driver.get(‘http://www.baidu.com‘) try: driver.find_element_by_id(‘kw‘).send_keys(‘ss‘) except NoSuchElementException as e: print(e)
窗口/frame切换
#coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary(‘/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin‘) driver = webdriver.Firefox(firefox_binary=binary) driver.get(‘http://www.baidu.com‘) #iframe切换id或name,否则用XPATH定位 driver.switch_to.frame("if") #返回之前页面 driver.switch_to.parent_frame() # 多窗口切换 #获得当前窗口句柄 search_windows=driver.current_window_handle driver.find_element_by_link_text(‘登录‘).click() driver.find_element_by_link_text(‘立即注册‘).click() #获取当前所有打开的窗口句柄 all_handles = driver.window_handles #进入注册窗口 for handle in all_handles: if handle != search_windows: driver.switch_to.window(handle) print(‘注册。。。‘) # 填写信息send_keys #回到之前窗口 for handle in all_handles: if handle ==search_windows: driver.switch_to.window(handle) print(‘搜索。。。‘)
标签:send form element coding class lin 返回 xpath evel
原文地址:http://www.cnblogs.com/Erick-L/p/7648750.html