自动化测试,超有用。
PROXY,PLUGIN,PROFILE,WINDOWS HANDLE个个搞定!!!
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
选择浏览器: driver = webdriver.Firefox()
打开url: driver.get("http://www.baidu.com")
等待: driver.implicitly_wait(30)
driver.set_page_load_timeout(30)
driver.set_script_timeout(30)
关闭浏览器: driver.quit()/driver.close()
前进/后退: driver.forward()/driver.back()
刷新: driver.refresh()
返回当前页面标题:driver.title
返回当前页面url:driver.current_url
返回当前浏览器的所有窗口:driver.window_handles
返回当前浏览器的窗口句柄:driver.current_window_handle
选择窗口: driver.switch_to_window(“window_name”)
对话框操作: driver.switch_to_alert() #选择窗口对象
accept() #点击确认
dismiss() #点击取消
text #获取文本值
send_keys(“key”) #输入值
定位元素(单个):driver.find_element(by=”id”,value=None)
Id定位: driver.find_element_by_id(“id_name”)
Name定位: driver.find_element_by_name(“name”)
Css定位: driver.find_element_by_css_selector(“#foo”)
Xpath定位:driver.find_element_by_xpath(“//select[@id=’nr’]/option[2]”)
Link定位: driver.find_element_by_link_text(u”链接”)
Tag定位: driver.find_element_by_tag_name(“foo”)
截取当前页面: driver.get_screenshot_as_file('/Screenshots/foo.png')
来一段。。。
profileDir = "C:/xxx/AppData/Roaming/Mozilla/Firefox/Profiles/xxx.default" profile = webdriver.FirefoxProfile(profileDir) profile.set_preference('network.proxy.type', 1) profile.set_preference('network.proxy.http', proxy[0]) profile.set_preference('network.proxy.http_port', int(proxy[1])) profile.set_preference('network.proxy.ssl', proxy[0]) profile.set_preference('network.proxy.ssl_port', int(proxy[1])) profile.update_preferences() browser = webdriver.Firefox(profile) try: browser.get(url)) #browser.set_page_load_timeout(10) #browser.set_script_timeout(10) main_handle = browser.current_window_handle for i in range(1,x): link = browser.find_elements_by_tag_name("a") if link.is_displayed() == False: continue url = link.get_attribute("href") if (url is None) : continue link.click() logging.info( ) all_handles = browser.window_handles for handle in all_handles: if handle != main_handle: browser.switch_to_window(handle) browser.close() browser.switch_to_window(main_handle) else: browser.back() browser.quit()