| | 1 | 论坛版块页的发帖按钮,注册了onmouse事件,当鼠标移入的时候,会弹出发帖类型菜单供选择。如下图: |
| | 2 | [[BR]] |
| | 3 | [[Image(pop_menu.png)]] |
| | 4 | [[BR]] |
| | 5 | 如何编写脚本来实现呢? |
| | 6 | [[BR]] |
| | 7 | selenium API中提供了ActionChains对象用来处理注册事件,首先要实例化一个ActionChains对象,然后调用该对象的move_to_element方法将mouse移动指定的元素上 |
| | 8 | [[BR]] |
| | 9 | 示例代码如下: |
| | 10 | {{{ |
| | 11 | ...... |
| | 12 | #找到发帖按钮 |
| | 13 | btn = driver.find_element_by_xpath("//div[@id='content']/div[@class='forumBar cle']/div[@class='btn_post']") |
| | 14 | btn.location_once_scrolled_into_view |
| | 15 | chains = ActionChains(driver) |
| | 16 | #鼠标移到发帖按钮 |
| | 17 | chains.move_to_element(btn).perform() |
| | 18 | #点击发投票帖 |
| | 19 | vote_btn = driver.find_element_by_xpath("//div[@id='content']/div[@class='forumBar cle']/div[@class='btn_post']/ul/li[@class='vote']") |
| | 20 | vote_btn.click() |
| | 21 | ...... |
| | 22 | }}} |
| | 23 | 注意,别忘了要调用perform()方法,来执行元素上注册的事件 |