参考文档:
Selenium with Python英文文档
Selenium with Python中文翻译文档
from selenium import webdriver from selenium.common.exceptions import TimeoutException, NoSuchElementException import time # 声明浏览器对象 driver = webdriver.Chrome() # 可用的浏览器对象: # Firefox,Chrome,Ie,Edge,Opera,Safari,BlackBerry,PhantomJS,Android # 访问页面 def foo1(): driver.get("http://www.baidu.com") print(driver.page_source) # 查找元素 try: p=driver.find_element_by_class_name("p") except NoSuchElementException as e: print(e) """ # 单个元素 find_element_by_id find_element_by_name find_element_by_xpath find_element_by_tag_name find_element_by_link_text find_element_by_class_name find_element_by_css_selector find_element_by_partial_link_text # 多个元素 find_elements_by_id find_elements_by_name find_elements_by_xpath find_elements_by_tag_name find_elements_by_link_text find_elements_by_class_name find_elements_by_css_selector find_elements_by_partial_link_text """ # 通用查找方式 from selenium.webdriver.common.by import By driver.find_element(By.ID, "kw") """ ID = "id" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" NAME = "name" TAG_NAME = "tag name" CLASS_NAME = "class name" CSS_SELECTOR = "css selector" """ # 元素交互 def foo2(): import time driver.get("http://www.baidu.com") input = driver.find_element_by_id("kw") input.send_keys("淘宝") time.sleep(2) input.clear() input.send_keys("京东") button = driver.find_element_by_id("su") time.sleep(2) button.click() # 交互动作 # 将动作附加到动作链中串行执行 def foo3(): driver.get("http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable") # 切换到子frame driver.switch_to.frame("iframeResult") draggable = driver.find_element_by_id("draggable") droppable = driver.find_element_by_id("droppable") actions = webdriver.ActionChains(driver) actions.drag_and_drop(draggable, droppable) actions.perform() # 切换回父frame driver.switch_to.parent_frame() # 执行javascript def foo4(): driver.execute_script('alert("hello")') # 获取文本和属性 def foo5(): driver.get("https://www.zhihu.com/topics") # 隐式等待 driver.implicitly_wait(10) # 显示等待 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By wait = WebDriverWait(driver, 10) logo = wait.until(EC.presence_of_element_located((By.ID, "zh-top-link-logo"))) print(logo.text) # 获取属性, id, 标签名, 位置, 大小 print(logo.get_attribute("class")) print(logo.id) print(logo.location) print(logo.tag_name) print(logo.size) # 前进后退 def foo6(): import time driver.get("http://www.taobao.com") driver.get("http://www.jd.com") driver.get("http://www.qq.com") time.sleep(2) driver.back() time.sleep(2) driver.forward() time.sleep(3) # cookie def foo7(): driver.get("https://www.zhihu.com/explore") print(driver.get_cookies()) driver.add_cookie({"name": "mouday", "value": "mouday.com"}) print(driver.get_cookies()) driver.delete_all_cookies() print(driver.get_cookies()) # 选项卡管理 def foo8(): driver.get("https://www.qq.com/") # 开启 driver.execute_script("window.open()") print(driver.window_handles) # 切换 driver.switch_to_window(driver.window_handles[1]) driver.get("https://www.taobao.com/") time.sleep(2) # 切换 driver.switch_to_window(driver.window_handles[0]) driver.get("https://www.jd.com/") time.sleep(2) # 异常处理 def foo9(): try: driver.get("https://www.google.com") except TimeoutException as e: print(e.msg) try: p = driver.find_element_by_class_name("p") except NoSuchElementException as e: print(e) # 关闭退出浏览器 driver.close() driver.quit() # 如果不退出浏览器,会报错: # ImportError: sys.meta_path is None, Python is likely shutting down