Summary of common methods of Python Selenium (continuously added)

This article also enriches the learning and introduction of common element positioning methods and operations in Selenium

selenium Python summarizes some APIs that may be used frequently in work.

  • 1. Get the Url of the current page
方法:current_url  
实例:driver.current_url

 

 


  • 2. Get element coordinates
方法:location
解释:首先查找到你要获取元素的,然后调用location方法
实例:driver.find_element_by_xpath("xpath").location

  • 3. Submission of the form
方法:submit
解释:查找到表单(from)直接调用submit即可
实例:driver.find_element_by_id("form1").submit()

 


  • 4. Get the property value of CSS
方法:value_of_css_property(css_name)
实例:driver.find_element_by_css_selector("input.btn").value_of_css_property("input.btn")

  • 5. Get the attribute value of the element
方法:get_attribute(element_name)
实例:driver.find_element_by_id("kw").get_attribute("kw")

  • 6. Determine if an element is selected
方法:is_selected()
实例:driver.find_element_by_id("form1").is_selected()

  • 7. Return the size of the element
方法:size
实例:driver.find_element_by_id("iptPassword").size
返回值:{'width': 250, 'height': 30}

  • 8. Determine whether the element is displayed
方法:is_displayed()
实例:driver.find_element_by_id("iptPassword").is_displayed()

  • 9. Determine if an element is used
方法:is_enabled()
实例:driver.find_element_by_id("iptPassword").is_enabled()

  • 10. Get the text value of an element
方法:text
实例:driver.find_element_by_id("iptUsername").text

  • 11. Element assignment
方法:send_keys(*values)
实例:driver.find_element_by_id("iptUsername").send_keys('admin')

  • 12. Return the tagName of the element
方法:tag_name
实例:driver.find_element_by_id("iptUsername").tag_name

  • 13. Delete all cookies from your browser
方法:delete_all_cookies()
实例:driver.delete_all_cookies()

  • 14. Delete the specified cookie
方法:delete_cookie(name)
实例:deriver.delete_cookie("my_cookie_name")

  • 15. Close your browser
方法:close()
实例:driver.close()

  • 16. Close the browser and exit the driver
方法:quit()
实例:driver.quit()

  • 17. Back to previous page
方法:back()
实例:driver.back()

  • 18. Empty the input box
方法:clear()
实例:driver.clear()

  • 19. Maximize the browser window
方法:maximize_window()
实例:driver.maximize_window()

  • 20. Check the browser's name
方法:name
实例:drvier.name

  • 21. Return cookies in the current session
方法:get_cookies()
实例:driver.get_cookies()

  • 22. Find the mapped Value value based on the cookie name
方法:driver.get_cookie(cookie_name)
实例:driver.get_cookie("NET_SessionId")

  • 23. Capture the current page
方法:get_screenshot_as_file(filename)
实例:driver.get_screenshot_as_file("D:\\Program Files\\Python27\\NM.bmp")

  • 24. Get the coordinates of the current window
方法:get_window_position()
实例:driver.get_window_position()

  • 25. Get the length and width of the current window
方法:get_window_size()
实例:driver.get_window_size()

Common methods of mouse operation of ActionChains class:

引入ActionChains类:from selenium.webdriver.common.action_chains import ActionChains
  • 26. Right click
方法:context_click()
实例:ActionChains(driver).context_click(driver.find_element_by_id("id")).perform()

  • 27. Double click
方法:double_click()
实例:ActionChains(driver).double_click(driver.find_element_by_name("name")).perform()

  • 28: Mouse drag and drop
方法:drag_and_drop(source, target)
     source:鼠标按下的源元素;target:鼠标释放的目标元素
实例:element = driver.find_element_by_name("name")
     target = driver.find_element_by_name("name")
     ActionChains(driver).drag_and_drop(element, target).perform()

  • 29: Mouse over an element (hover)
方法:move_to_element()
实例:above = driver.find_element_by_xpath("xpath路径")
      ActionChains(driver).move_to_element(above).perform()

  • 30: Press the left mouse button on an element
方法:click_and_hold()
实例:left = driver.find_element_by_name("name")
     ActionChains(driver).click_and_hold(left).perform()

Keyboard events:

引入Keys类包:from selenium.webdriver.common.keys import Keys

  • 31: Input
方法:send_keys()
实例:driver.find_element_by_id("id").send_keys("XXX")

  • 32: Enter a space
方法:send_keys(Keys.SPACE)
实例:driver.find_element_by_id("id").send_keys(Keys.SPACE)

  • 33: ctrl + a 
    select all the content of the input box 
    ctrl + x 
    cut the content of the input box 
    ctrl + v 
    paste to the input box 
    ctrl + c 
    copy
方法:send_keys(Keys.CONTROL,'a')
实例:driver.find_element_by_id("id").send_keys(Keys.CONTROL,'a')

  • 34: Enter instead of clicking
方法:send_keys(Keys.ENTER)
实例:driver.find_element_by_id("id").send_keys(Keys.ENTER)

  • 35: Tab key (Tab)
方法:send_keys(Keys.TAB)
实例:driver.find_element_by_id("id").send_keys(Keys.TAB)

  • 36: Back key (Esc)
方法:send_keys(Keys.ESCAPE)
实例:driver.find_element_by_id("id").send_keys(Keys.ESCAPE)

waiting time

import WebDriverWait package 
from selenium.webdriver.support.ui import WebDriverWait 
import time package 
import time

  • 37: Fixed wait time
方法:sleep()
实例:time.sleep(5)  # 等待5秒

  • 38: Wait for an element to be found, or a command to complete, and throw an exception intelligent wait if the set time is exceeded.
方法:implicitly_wait()
实例:driver.implicitly_wait(30)

  • 39: Within the set time, by default, it will check whether the current page element exists at regular intervals. If it cannot be detected after the set time, an exception will be thrown.
"方法:WebDriverWait()"

#WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
#——driver:WebDriver的驱动程序(Ie, Firefox, Chrome或远程)
#——timeout:最长超时时间,默认以秒为单位
#——poll_frequency:休眠时间的间隔(步长)时间,默认为0.5秒 #——ignored_exceptions:超时后的异常信息,默认情况下抛NoSuchElementException异常 实例: element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("id")) #一般由unit()或until_not()方法配合使用,同上: 调用该方法提供的驱动程序作为一个参数,直到返回值不为False。 ——until(method, message=’’) 调用该方法提供的驱动程序作为一个参数,直到返回值为False。 ——until_not(method, message=’’) # 还可以与expected_conditions 一起使用 from selenium.webdriver.support import expected_conditions as EC # 实例: 判断某个元素是否可见并且是enable的,这样才clickable WebDriverWait(dr,15,1).until(EC.element_to_be_clickable((By.ID,"EmployeeListMenu")),"Not Find element")

  • 40: Select all tags on the current page
name为input的元素
inputs = driver.find_elements_by_tag_name(‘input‘)

  • 41: Filter out the element whose type is checkbox and check it
for input in inputs:
    if input.get_attribute(‘type‘) == ‘checkbox‘:
        input.click()

  • 42: Use CSS positioning to select all elements of type checkbox and check them
checkboxes = driver.find_elements_by_css_selector(‘input[type = checkbox]‘)
for checkbox in checkboxes:
      checkbox.click()

  • 43: Uncheck the last checkbox. When the pop() method has an empty parameter, the last element in the list is removed by default.
driver.find_elements_by_css_selector(‘input[type = checkbox]‘).pop().click()

toggle active object

  • 44: Switch browser handle
# 切换不同的tab页
方法:driver.switch_to.window(window_name)
# 备注:从A页跳转到B页,句柄已经切换过去,但是焦点没有切过去,所以需要switch_to.window,把焦点也切过去,才可以在当前页进行操作。
# 切换是思路,获取所有的句柄,因为返回是一个list,而且要切换的对象都是最后一个,可以使用[-1]直接切过去
# 例如:
driver.switch_to.window(driver.window_handles[-1])
  • 45: Return to the previous form
方法: driver.switch_to_parent_content()#旧方法
      driver.switch_to.parent_content#新方法

  • 46: Return to the outermost form
方法: driver.switch_to_default_content()#旧方法
      driver.switch_to.default_content()#新方法

  • 47: Switch to the specified frame
方法:driver.switch_to.frame('xxx')
实例:driver.switch_to.frame('frame_name')
     driver.switch_to.frame(index)
     driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])        

  • 48: Get the title of the current page
方法:driver.title
实例:driver.title

  • 49: The focus is switched to the pop-up window.
方法:driver.switch_to_alert()
实例:driver.switch_to_alert()

  • 50: Forward
方法:
driver.forward()   

  • 51: Refresh the page
方法:driver.refresh() 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324518697&siteId=291194637