selenium API 随记

来源:互联网 发布:美得惊为天人知乎 编辑:程序博客网 时间:2024/06/04 20:15

webdriver

查找元素

  • find_element_by_id
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_name
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

所有的以上操作,都有查找多个元素的对应操作find_elements_by_[option]

获取内容

操作 注释 get Loads a web page in the current browser session. title Returns the title of the current page. current_url Gets the URL of the current page. page_source Gets the source of the current page. get_screenshot_as_file(filename) Gets the screenshot of the current window. Returns False if there is any IOError, else returns True. Use full paths in your filename. get_screenshot_as_png Gets the screenshot of the current window as a binary data. get_screenshot_as_base64 Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML. get_log(log_type) Gets the log for a given log type log_types Gets a list of the available log types

窗口操作

操作 注释 close Closes the current window. quit Quits the driver and closes every associated window. current_window_handle Returns the handle of the current window. window_handles Returns the handles of all windows within the current session. maximize_window Maximizes the current window that webdriver is using

执行js脚本

操作 注释 execute_script(script, *args) Synchronously Executes JavaScript in the current window/frame.
- script: The JavaScript to execute.
- *args: Any applicable arguments for your JavaScript. execute_async_script(script, *args) Asynchronously Executes JavaScript in the current window/frame.

cookie相关

操作 注释 get_cookie(name) Get a single cookie by name. Returns the cookie if found, None if not. delete_cookie(name) Deletes a single cookie with the given name. delete_all_cookies Delete all cookies in the scope of the session. add_cookie(cookie_dict) Adds a cookie to your current session.
- cookie_dict: A dictionary object, with required keys - “name” and “value”;
optional keys - “path”, “domain”, “secure”, “expiry”

超时设置

操作 注释 implicitly_wait(time_to_wait) Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session. To set the timeout for calls to execute_async_script, see set_script_timeout. set_script_timeout Set the amount of time that the script should wait during an execute_async_script call before throwing an error. set_page_load_timeout Set the amount of time to wait for a page load to complete before throwing an error.

webelement

操作 注释 tag_name This element’s tagName property. text The text of the element. click Clicks the element. submit Submits a form. clear Clears the text if it’s a text entry element. get_attribute(name) Gets the given attribute or property of the element. name - Name of the attribute/property to retrieve. If there’s no attribute with that name, None is returned. is_selected Returns whether the element is selected. Can be used to check if a checkbox or radio button is selected. is_enabled Returns whether the element is enabled. find_element_by_[option] Finds element within this element’s children by option send_keys(value) Simulates typing into the element. - value - A string for typing, or setting form fields. For setting file inputs, this could be a local file path. size The size of the element. value_of_css_property The value of a CSS property. location The location of the element in the renderable canvas. rect A dictionary with the size and location of the element. screenshot_as_base64 Gets the screenshot of the current element as a base64 encoded string. screenshot_as_png Gets the screenshot of the current element as a binary data. screenshot Gets the screenshot of the current element. Returns False if there is any IOError, else returns True. Use full paths in your filename. parent Internal reference to the WebDriver instance this element was found from.
0 0
原创粉丝点击