# selenium **Repository Path**: new_fish_learn_python/selenium ## Basic Information - **Project Name**: selenium - **Description**: selenium的系统学习 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-04-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### selenium 学习 1.元素定位 1.1 定位元素返回一个值 find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_tag_name find_element_css_selector 1.2 定位元素返回一个list find_elements_by_id find_elements_by_name find_elements_by_xpath find_elements_by_link_text find_elements_by_tag_name find_elements_css_selector 2. 设置等待 2.1 time.sleep() 3. 动作链:是一些系列复杂动作的集合。 常用地的动作方法 3.1 click() 左键单击传入的元素,如果不传,点击鼠标当前位置 3.2 context_click() 右键单击 3.3 double_click() 双击 3.4 drag_and_hold() 点击并抓起 3.5 drag_and_drop(source,target)在source元素上点击抓起,移动到target元素上松开放下 3.6 drag_and_drop_by_offset(source,xoffset,yoffset) 在source元素上点击抓起,移动到相对于source元素偏移xoffset和yoffset的坐标位置放下 3.7 send_keys(*key_to_send) 将关键词发送到当前聚焦的元素 3.8 send_keys_to_element(element,*keys_to_send) 将关键词发送到指定的元素 3.9 reset_actions() 清除已经存储的动作 4. 可以用execute_script()来实现一些selenium API没有提供的功能 5. 获取节点信息 5.1 获取属性 get_attribute() browser.get(url) logo = browser.find_element_by_id('zh-top-link-logo') print(logo.get_attribute('class')) 5.2 获取文本值 每个webElement节点都有text属性 print(logo.text) 5.3 获取id,位置,标签名和大小 print(logo.id) print(logo.location) print(logo.tag_name) print(logo.size) 6. 切换Frame browser.switch_to_frame('iframeResult') # 进入指定的页面'iframeResult' browser.switch_to_window('window_name') # 切换到指定的window_name页面 browser.switch_to_alert() # 切换到alert弹窗 browser.switch_to_active_element() # 定位到当前聚焦的元素上 browser.switch_to_default_content # 切换到最上层页面 browser.switch_to_frame(frame_reference='') # 通过id,name,element(定位的某个元素),索引来切换某个frame browser.switch_to.parent_frame() # 退回到上一层某个frame, 对于层层嵌套的frame很有用 7.延时等待 7.1 隐式等待:当查到节点时,节点没有立即出现时,隐式等待将等待一段时间在查找 会受到网络条件的影响 默认的时间是0 browser.implicitly_wait() 7.2 显示等待:指定要查找的节点,然后指定一个最长等待时间,返回查到的节点,如果在规定 时间没有加载出该节点,则抛出异常 webDriverWait(browser,10) input = wait.until(EC.presence_of_element_located(By.ID,'q')) button = wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR,'.btn-search')) 判断条件: title_is 标题是某内容 title_contains 标题包含某内容 presence_of_element_located 节点加载起来,传入定位元组,如By.ID,'p' visibility_of_element_located 节点可见,传入定位元组 visibility_of 可见,传入节点对象 presence_of_all_element_located 所有节点加载出来 text_to_be_present_in_element 某个节点文本包含某文字 text_to_be_present_in_element_value 某个节点值包含某个文字 frame_to_be_available_and_switch_to_it 加载并切换 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 elemnet_laocated_selection_state_to_be 传入定位元组及状态,相等返回True,否则返回False alert_is_persent 是否出现警告 8.浏览器的前进和后退 browser.get(url) browser.back() 后退 browser.forward() 前进 9.cookies的操作 browser.get(url) browser.get_cookies() # 获取cookies browser.add_cookies({"name": name, "domain": "www.zhihu.com","value": "germey""}) # 增加cookies browser.delete_all_cookies() # 删除cookies 10. 选项卡管理 browser.get(url) browser.execute.script('window.open()') # 开启新的选项卡 print(browser.window_handles) browser.switch 11.异常处理:需要对节点未找到的异常,超时异常进行捕获