Mouse events:
Actionchains class
? Context_click () Right-click
? Double_click () Double-click
? Drag_and_drop () drag
right mouse button:
From selenium.webdriver.common.action_chains import Actionchains #导入ActionChains包
# Navigate to the element you want to right-click qqq=driver.find_element_by_xpath ("/html/body/div/div[2]/div[2]/ Div/div[3]/table/tbody/tr/td[2]# performs a right mouse button action on the anchored element Actionchains ( Driver). Context_click (QQQ). Perform ()"#你也可以使用三行的写法, but I think the above two lines are easier to understand chain = Actionchains ( Driver) Implement =driver.find_element_by_xpath ("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]") Chain.context_click (Implement). Perform ()"
Other actions of the mouse:
# perform a mouse double-click on the anchored element actionchains (Driver). Double_click (QQQ). Perform () # performing a move operation on an element Actionchains (Driver). Drag_and_drop (element, target). Perform ()
To locate a group of elements:
fromSeleniumImportWebdriverImport TimeImportOSDR=Webdriver. Firefox () File_path='file:///'+ Os.path.abspath ('checkbox.html') Dr.get (file_path)#Select all input on the page, then filter out all the checkboxes and tick theInputs = Dr.find_elements_by_tag_name ('input' ) forInputinchInputs:ifInput.get_attribute ('type') =='checkbox': Input.click () time.sleep (2) Dr.quit ()
Method Two:
#-*-coding:utf-8-*- fromSeleniumImportWebdriverImport TimeImportOSDR=Webdriver. Firefox () File_path='file:///'+ Os.path.abspath ('checkbox.html') Dr.get (file_path)#Select all the checkboxes and tick them all.checkboxes = Dr.find_elements_by_css_selector ('Input[type=checkbox]' ) forCheckBoxinchCheckboxes:checkbox.click () time.sleep (2)#Print the number of checkboxes on the current pagePrintLen (Dr.find_elements_by_css_selector ('Input[type=checkbox]')) Time.sleep (2) Dr.quit ()
# Remove the Dr.find_elements_by_css_selector ('input[type=checkbox]' from the last 1 checkboxes on the page ). Pop (). Click () time.sleep (2)
Frame or window positioning:
Browser.switch_to_frame ("F1")# then find the ifrome2 below it (ID =f2) Browser.switch_to_frame ("f2")
#下面就可以正常的操作元素了
Driver.switch_to_window ("Windowname")
Selenium Webdriver (3)