#将鼠标移动到二级元素位置
# -*- coding: utf-8 -*-
from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWait
import time
import os
dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('level_locate.html')
dr.get(file_path)
dr.find_element_by_link_text('Link1').click()
WebDriverWait(dr, 10).until(lambda the_driver: the_driver.find_element_by_id('dropdown1').is_displayed())
menu = dr.find_element_by_id('dropdown1').find_element_by_link_text('Another action')
webdriver.ActionChains(dr).move_to_element(menu).perform()
time.sleep(2)
dr.quit()
html文件:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Level Locate</title>
<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
</head>
<body>
<h3>Level locate</h3>
<div class="span3">
<div class="well">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Link1</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" id="dropdown1" >
<li><a tabindex="-1" href="#">Action</a></li>
<li><a tabindex="-1" href="#">Another action</a></li>
<li><a tabindex="-1" href="#">Something else here</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
<div class="span3">
<div class="well">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Link2</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" >
<li><a tabindex="-1" href="#">Action</a></li>
<li><a tabindex="-1" href="#">Another action</a></li>
<li><a tabindex="-1" href="#">Something else here</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
#点击二级元素的按钮
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os
import selenium.webdriver.support.ui as ui
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('button_dropdown.html')
dr.get(file_path)
# 定位text是watir-webdriver的下拉菜单
# 首先显示下拉菜单
dr.find_element_by_link_text('Info').click()
wait = ui.WebDriverWait(dr, 10)
wait.until(lambda dr: dr.find_element_by_class_name('dropdown-menu').is_displayed())
# 通过ul再层级定位
dr.find_element_by_class_name('dropdown-menu').find_element_by_link_text('watir-webdriver').click()
sleep(1)
dr.quit()
html文件:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>button dropdown</title>
<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(
function(){
$('.dropdown-menu').find('a').first().click(function(){ alert('watir-webdriver is better than selenium-webdriver'); });
}
);
</script>
</head>
<body>
<h3>button dropdown</h3>
<div class="row-fluid">
<div class="span3">
<div class="well">
<div class="btn-group">
<a class="btn dropdown-toggle btn-info" data-toggle="dropdown" href="#">
Info
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">watir-webdriver</a></li>
<li><a href="#">better than</a></li>
<li><a href="#">selenium-webdriver</a></li>
</ul>
</div>
</div>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>