单击错误按钮,页面上有两个按钮

时间:2013-04-03 11:22:13

标签: selenium-webdriver

我正在使用Selenium Webdriver来测试网页。 网页http://www.leaseplan.nl/contact/index.asp有两个按钮,一个按钮带有按钮文本'Zoeken',另一个按钮带有按钮文本'Verstuur'。我想通过使用XPath或CssSelector以及以下代码点击带有按钮文本'Verstuur'的按钮:

driver.FindElement(By.XPath("/html/body/div[3]/div[2]/div[2]/form/fieldset/a/span")).Click();
driver.FindElement(By.CssSelector("fieldset.contact_form > a.button > span.button_center")).Click();

但是,使用上述任一行代码,点击带有文字'Zoeken'的按钮。

此按钮具有非常相似的CssSelector和XPath:

fieldset.header_search a.button span.button_center

/html/body/div[3]/div/form/fieldset/a/span[2]

有人知道如何解决这个问题吗?

4 个答案:

答案 0 :(得分:1)

尝试使用cssSelector并告诉我它是否有效。 对于“verstuur”:

By.cssSelector("div.content form a.button")

解决方案:

好的,我发现了你的问题。你的xpath很好但是现在,你对点击的操作提交了第一个表单,所以表单带有“Zoeken”。

onclick="document.forms[0].submit();" // submit the 1st form, the bad one !

试试这个:

onclick="document.forms["form"].submit();" // submit the 2nd form, the good one !
//or
onclick="document.forms[1].submit();"

证明:Jsfiddle

答案 1 :(得分:0)

ZOEKEN /html/body/div[3]/div/form/fieldset/a/span[2]

的xpath

VERSTUUR /html/body/div[3]/div[2]/div[2]/form/fieldset/a/span[2]

的xpath

答案 2 :(得分:0)

试试这个:

driver.FindElement(By.XPath("//span[text()='Verstuur']")).click();

编辑:

我认为你想学习Selenium,这就是你使用第三方网站的原因。如果你真的想学习Selenium,可以找一些自动化的开源应用程序。这是一个很好的自动化应用程序 - http://sourceforge.net/projects/sugarcrm/files/1%20-%20SugarCRM%206.5.X/FastStack/

下载最新版本并安装。

在此处查找有关网站和selenium的更多信息。真是太棒了..

http://selftechy.com/2011/02/05/introduction-to-selenium-web-application-test-automation-tool

答案 3 :(得分:0)

Zoeken:driver.findElement(By.xpath("//div[1]/form/fieldset/a/span[2]")).click();

Verstuur:driver.findElement(By.xpath("//div[2]/form/fieldset/a/span[2]")).click();

使用xpath很容易但很慢。