In this article, I sumamrize some selenium webdriver api command to help coding webspider.
locating element
selenium webdriver offer several ways to locate the elements :
- id
- name
- class name
- tag name
- link text
- partial link text
- xpath
- css selector
coressponding to the following :
- 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()
following the above locating element, we have four most commonly used operations:
- clear clear the content, if possible
- send_keys simulate the keyboard input operation. But if we want to input chinese chacarters, usually we declare “utf-8” format at the beginning and use send_keys(u”中文内容”)
- click simulate the mouse click operation, can click any text/image link, button, option …
- submit submit a form, but the object should be a form
for example, the following is a typical case for user to login a website:
1 |
|
please note that if we use find_elements_by… , we get a group of info which has the key word info. Then we can use loop to select a group of elements that we want.
keyboard operation
most commonly used keyboard function key :
1 |
|
we can also use webdriver to get page information, such as title, url link, text content. For example:
1 |
|
multiple windows operation
If we need to switch to multiple windows in a single webpage, we can use current_window_handle, window_handles,switch_to_window() command to operate.
javascript operation
generally, we can use driver.execute_script() to apply javascript command
scrollbar operation, sometimes (sign up for a website ), we need to control the scrollbar1
2
3
4
5
6
7
8
9
10
11code for scroll:
<body onload= "document.body.scrollTop=0 ">
<body onload= "document.body.scrollTop=100000 ">
#将页面滚动条拖到底部
js = "var q=document.documentElement.scrollTop=10000"
driver.execute_script(js)
time.sleep(3)
#将滚动条移动到页面的顶部
js_ = "var q=document.documentElement.scrollTop=0"
driver.execute_script(js_)
cookie operation
1 |
|