Python browser simulation of selenium

Python browser simulation of selenium

1.selenium selected element method
  • find_element_by_id: By selecting element id, for example: driver.find_element_by_id ( 'loginForm')
  • find_element_by_name: name by selecting elements, driver.find_element_by_name ( 'password')
  • find_element_by_xpath:通过xpath选择,driver.find_element_by_xpath(“//form[1]”)
  • find_element_by_link_text: by selecting the link address
  • find_element_by_partial_link_text: by selecting the link portion of the address
  • find_element_by_tag_name: Select elements by name
  • find_element_by_class_name: the element's id selectors
  • find_element_by_css_selector: selected by the selector css

Sometimes, we need to find multiple elements, so there are elements of the corresponding selection method, that is, after the above-mentioned element plus s, become elements. Example: find_elements_by_name

2. The method of operating element selenium automate web

First, find the object that you want to be executed by the method of selecting the element selenium, then that object operation. Common operating elements as follows:
- the Clear to clear the contents of elements
- send_keys analog key input
- click click elements
- submit to submit the form
to invoke the keyboard input should be introduced package from selenium.webdriver.common.keys import Keys

3.selenium difference in close and quit the

Close (): Closes the current window .
Close the current window.

Quit ():. Quits the driver and closes every associated window
to exit the drive and close all windows associated.

Label switching 4.frame

frame labels frameset, frame, iframe three kinds, frameset is no different with other common label, will not affect the normal position, while the frame and iframe for selenium is the same in terms of positioning, selenium has a set of methods for frame operation.

1 driver.switch_to.frame(reference)  #切入
2 driver.switch_to.parent_frame()    #从子frame切回到父frame
3 driver.switch_to.default_content() #切回
other
#打印页面标题
print driver.title

#生成当前页面快照
driver.save_screenshot("baidu.png")

# id="kw"是百度搜索框,输入字符串“微博”,跳转到搜索中国页面
driver.find_element_by_id("kw").send_keys(u"微博")

# id="su"是百度搜索按钮,click() 是模拟点击
driver.find_element_by_id("su").click()

# 获取新的页面快照
driver.save_screenshot(u"微博.png")

# 打印网页渲染后的源代码
print driver.page_source

# 获取当前页面Cookie
print driver.get_cookies()

# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')

# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')

# 输入框重新输入内容
driver.find_element_by_id("kw").send_keys("test")

# 模拟Enter回车键
driver.find_element_by_id("su").send_keys(Keys.RETURN)

# 清除输入框内容
driver.find_element_by_id("kw").clear()

# 生成新的页面快照
driver.save_screenshot("test.png")

# 获取当前url
print driver.current_url


Published 33 original articles · won praise 1 · views 2298

Guess you like

Origin blog.csdn.net/qq_40805620/article/details/98944506