selenium

selenium

webDriver原理

工作流程

  • 打开浏览器并绑定到指定端口,作为remote server
  • client通过commandExecuter发送httpRequest给remote server
  • remote server 根据原生的浏览器组件来转化为浏览器的本地调用

    元素定位

  • find_element_by_id

  • find_element_by_name

  • find_element_by_class_name

  • find_element_by_tag_name

  • find_element_by_link_text

  • find_element_by_partial_link_text

  • find_element_by_xpath 绝对定位

  • find_element_by_css_selector

    常见控件

    button

    element.click()

    Textbox,Upload

    element.send_keys()

    MouseEvent

  • double_click()

  • context_click()

  • drag_and_drop()

  • move_to_element()

  • perform()

  • select_by_index()

  • select_by_value()

  • select_by_visible_text()

    Alert

  • switch_to.alert.accept()

  • switch_to.alert.dismiss()

  • switch_to.alert.text()

  • switch_to.alert.send_keys()

    Window

  • Driver.refresh()

  • Driver.back()

  • Driver.forward()

  • Driver.maximize_window()

  • Driver.set_window_size(100, 200)

  • Driver.switch_to.window()

    Frame

  • Driver.switch_to.frame()

  • Driver.switch_to.parent_frame()

  • Driver.switch_to.default_content()

    等待

  • 显示等待

    • WebDriverWait(driver, 5,0.5).until(EC.presence_of_element_located((By.ID, ‘kw’))
  • 隐式等待

    • driver.implicitly_wait(20)
  • 强制等待

    • time.sleep(1)
  • 截图

    • driver.get_screenshot_as_file()