使用xpath定位元素并单击它

时间:2013-05-15 08:22:23

标签: java selenium-webdriver

在java中使用selenium,无法单击按钮。如何使用xpath定位元素并单击它?

以下是我正在使用的源页面的一部分:

 
<div id="top-navigation">
<ul class="menu">
<li class="item first active">
<a href="/index.php/sprachen-lernen/englisch">Home</a></li>
<li class="item ">
<a href="/index.php/openArea/school">Online Language School
                </a></li>
<li class="item ">
<a href="/index.php/openArea/partner">Enterprise Solutions
                </a></li>
<li class="item ">
<a href="/index.php/openArea/customer">References
                </a></li>
<li class="item last ">
<a href="/index.php/openArea/contact">Contact</a></li>
</ul>
    <div id="login-button">
                    <a href="/login/login"><img src="/images/openArea/login.png" alt="Login" /></a>
            </div>
</div>

以下是我正在使用的声明

driver.findElement(By.id("login-button")).click();

但是它没有点击按钮。这是我第一次使用selenium,所以有人可以指导我说我的说法有问题,或者我想使用xpath然后应该是什么应该是确切的声明

1 个答案:

答案 0 :(得分:0)

我使用firebug来定位元素的xpath,现在它可以工作:)