‘‘‘ 驱动对象
http://selenium-python.readthedocs.io/ browser = webdriver.Chrome() browser = webdriver.Firefox() browser = webdriver.Edge() browser = webdriver.PhantomJS() browser = webdriver.Safari() ....等 访问页面 browser = webdriver.Chrome() browser.get(‘https://www.taobao.com‘) print(browser.page_source) 打印源码 browser.close() 关闭游览器 查找元素 单个元素 from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://www.taobao.com‘) # 4 种方式等价 input_by_id = browser.find_element(By.ID,‘q‘) 查找id 为q By.ID,By.CSS_SELECTOR input_first = browser.find_element_by_id(‘q‘) 查找id 为q input_second = browser.find_element_by_css_selector(‘#q‘) jq的方法查找id="q" input_third = browser.find_element_by_xpath(‘//*[@id="q"]‘) print(input_first, input_second, input_third) browser.close() 查找多个元素 from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://www.taobao.com‘) # 第一 lis = browser.find_elements_by_css_selector(‘.service-bd li‘) # 选择service-bd 下的 li元素 # 2 lis = browser.find_elements(By.CSS_SELECTOR,‘.service-bd li‘) # 等价的 print(lis) browser.close() ... find_elements_by_name find_elements_by_xpath find_elements_by_tag_name find_elements_by_class_name find_elements_by_css_selector 元素交互操作 from selenium import webdriver import time browser = webdriver.Chrome() browser.get(‘https://www.taobao.com‘) input = browser.find_element_by_id(‘q‘) input.send_keys(‘iPhone‘) time.sleep(1) input.clear() input.send_keys(‘iPad‘) button = browser.find_element_by_class_name(‘btn-search‘) button.click() 执行javascript from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://zhihu.com/explore‘) browser.execute_script(‘window.scrollTo(0,document.body.scrollHeight)‘) browser.execute_script(‘alert(‘To Botton‘)‘) 获取属性 logo = browser.find_element_by_id(‘zh-top‘) print(logo) print(logo.get_attribute(‘class‘)) # 打印class属性 获取文本值 input = browser.find_element_by_class_name(‘zu-top‘) print(input.text) 获取ID,位置,标签名,大小 input.id input.location # {y:‘‘,x:‘‘} input.tag_name # button input.size # {‘height‘: 22,‘width‘:11} ‘‘‘ # 元素交互操作 ‘‘‘ from selenium import webdriver import time browser = webdriver.Chrome() browser.get(‘https://taobao.com‘) input = browser.find_element_by_id(‘q‘) input.send_keys(‘iPhone‘) time.sleep(1) input.clear() input.send_keys(‘iPad‘) button = browser.find_element_by_class_name(‘btn-search‘) button.click() ‘‘‘ # 执行javascript ‘‘‘ from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://zhihu.com/explore‘) # 滚到底部 browser.execute_script(‘window.scrollTo(0,document.body.scrollHeight)‘) # 弹框 browser.execute_script(‘alert("I love You")‘) ‘‘‘ # iFrame 切换使用(页面内的iframe) ‘‘‘ import time from selenium import webdriver from selenium.common.exceptions import NoSuchElementException browser = webdriver.Chrome() url = ‘http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable‘ browser.get(url) browser.switch_to.frame(‘iframeResult‘) # 切换到这个iframe source = browser.find_element_by_css_selector(‘#draggable‘) print(source) try: # 在网页的iframe中不能 查找当前iframe之外的元素,需要切换 logo = browser.find_element_by_class_name(‘logo‘) # except: except NoSuchElementException as e: print(‘No Logo‘) # 切换iframe (切换到父iframe) browser.switch_to.parent_frame() logo = browser.find_element_by_class_name(‘logo‘) print(logo) print(logo.text) ‘‘‘ # 等待... # 隐士等待 ‘‘‘ from selenium import webdriver browser = webdriver.Chrome() browser.implicitly_wait(10) # 当没有找到元素,会等10秒在进行查找,如果有就不会等 browser.get(‘https://www.zhihu.com/explore‘) input = browser.find_element_by_class_name(‘zu-top-add-question‘) print(input) ‘‘‘ # 显示等待 ‘‘‘ from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC browser = webdriver.Chrome() browser.get(‘https://www.taobao.com‘) wait = WebDriverWait(browser, 10) input = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ‘.btn-search‘))) print(input) ‘‘‘ ‘‘‘ 显示等待的条件 http://selenium-python.readthedocs.io/installation.html title_is 标题是某内容 title_contains 标题包含某内容 presence_of_element_located((By.ID,‘p‘)) 元素加载出,传入定位元组, (By.ID,‘p‘) visibility_of_element_located 元素可见,传入定位元组 visibility_of 可见,传入元素对象 presence_of_all_elements_located 所有元素加载出 text_to_be_present_in_element 某个元素文本包含某个字 text_to_be_present_in_element_value 某个元素值包含某个字 frame_to_be_available_and_switch_to_it ifram加载并切换 invisibility_of_element_located 元素不可见 element_to_be_clickable 元素可点击 staleness_of 判断一个元素是否仍在DOM,可判断页面是否已经刷新 element_to_be_selected 元素可选择,传入定位元组 element_located_to_be_selected 元素可选择,传入定位元组 element_selection_state_to_be 传入元素对象以及状态, 相等返回True,否则返回False element_located_selection_state_to_be 传入定位元组以及状态,相等返回True,否则返回False alert_is_present 是否出现Alert ‘‘‘ # 游览器前进后退 ‘‘‘ import time from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://www.baidu.com‘) browser.get(‘https://www.taobao.com‘) browser.get(‘https://www.python.org‘) browser.back() # 后退 time.sleep(1) browser.forward() # 前进 browser.close() ‘‘‘ # cookie 获取和设置 ‘‘‘ from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://www.zhihu.com/explore‘) # 获取 print(browser.get_cookies()) # 添加 browser.add_cookie({‘name‘: ‘name‘,‘domain‘:‘www.zhihu.com‘,‘value‘:‘germey‘}) print(browser.get_cookies()) # 删除 browser.delete_all_cookies() print(browser.get_cookies()) ‘‘‘ # 选项卡(打开多个页面) ‘‘‘ import time from selenium import webdriver browser = webdriver.Chrome() browser.get(‘https://www.baidu.com‘) browser.execute_script(‘window.open()‘) print(browser.window_handles) # 当前有几个游览器窗口 browser.switch_to_window(browser.window_handles[1]) # 切换第二个 browser.get(‘https://www.taobao.com‘) time.sleep(1) browser.switch_to_window(browser.window_handles[0]) # 切换第一个 browser.get(‘https://python.org‘) ‘‘‘ # 异常处理 ‘‘‘ id不存在异常 from selenium import webdriver from selenium.common.exceptions import TimeoutException, NoSuchElementException browser = webdriver.Chrome() try: browser.get(‘https://www.baidu.com‘) except TimeoutException: print(‘Time out‘) try: browser.find_element_by_id(‘hello‘) except NoSuchElementException: print("No Element") finally: browser.close() ‘‘‘ from multiprocessing import Pool