This section focuses on:
Actionchains class
Context_click () Right-click
Double_click () Double-click
Drag_and_drop () drag
The test of the product is a right click on the file list will pop up a shortcut menu, you can easily select the shortcut menu to select the file to operate (delete, move, rename), before learning elements of the Click is very simple:
driver.find_element_by_id ("xxx"). Click ()
Then the mouse double-click, right click, drag, etc. is this the way it is written? For example, right-click:
driver.find_element_by_id ("xxx"). Context_click ()
The following error message was obtained after running the script:
Attributeerror: ' Webelement ' object has no attribute ' Context_click '
Tip the right point method does not belong to the Webelement object and finds a document that belongs to the Actionchains class, but does not have a specific wording in the document. Here to thank Beijing-qc-rabbit, in fact, the whole python+selenium learning process to thank Beijing-qc-rabbit guidance.
Here's how to use the right mouse button, as an example of a fast-seeding private cloud:
#coding =utf-8 from
Selenium import webdriver from
selenium.webdriver.common.action_chains Import Actionchains
Import time
driver = Webdriver. Firefox ()
driver.get ("Http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")
#登陆快播私有云
driver.find_element_by_id ("user_name"). Send_keys ("username")
driver.find_element_by_id (" User_pwd "). Send_keys (" 123456 ")
driver.find_element_by_id (" Dl_an_submit "). Click ()
time.sleep (3)
# 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]" )
#对定位到的元素执行鼠标右键操作
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 ()
"" Time.sleep (3) #休眠3秒
driver.close ()
It should be noted here that the package is introduced first before using the Actionchains class.
The right click action will be done, and the other methods below can also be written Bishi.
Double-click the mouse to the wording:
#定位到要双击的元素
qqq =driver.find_element_by_xpath ("xxx")
#对定位到的元素执行鼠标双击操作
actionchains (driver). Double_ Click (QQQ). Perform ()
Mouse drag-and-drop operation of the wording:
#定位元素的原位置
element = Driver.find_element_by_name ("source")
#定位元素要移动到的目标位置
target = driver.find_ Element_by_name ("target")
#执行元素的移动操作
actionchains (Driver). Drag_and_drop (element, target). Perform ()
The Actionchains class is more than just the three methods above, the following methods are listed:
Class Actionchains (Driver)
Driver:the Webdriver instance which performs user actions.
Generate user actions. All actions are stored in the Actionchains object. Call perform () to fire stored actions.
–perform ()
Performs all stored actions.
–click (On_element=none)
Clicks an element.
on_element:the element to click. If None, clicks on the current mouse position.
–click_and_hold (on_element)
Holds down the left mouse button on a element.
on_element:the element to mouse down. If None, clicks on the current mouse position.
–context_click (on_element)
Performs a context-click (right click) in an element.
on_element:the element to Context-click. If None, clicks on the current mouse position.
–double_click (on_element)
Double-clicks an element.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/