2017-04-25 143 views
-1

Xpath昨天工作正常,我想点击一个按钮。但它今天不工作。当试图运行脚本时,NoSuchElementException即将到来。下面我添加了代码和错误。Xpath昨天是完美的工作,但今天它不工作

HTML代码:

//Clicking on 'Add Customer' button 

driver.findElement(By.xpath(".//*[@id='userform']/section[1]/div/div[2]/input")).click(); 
Thread.sleep(5000); 

错误:

的 '添加用户' 按钮

<section class="content-header"> 
<div class="row"> 
<div class="col-xs-6"> 
<div class="col-xs-6 text-right"> 

<a class="btn btn-danger hidden checkdelbtn" data-content="Are you sure to continue?" data-title="Confirm" data-cancel="Cancel" data-confirm="Delete" data-form="userform" href=""> 

<input class="btn btn-primary" type="button" onclick="window.location.href='http://lab-1.sketchdemos.com/musicshop/stores/musicshop/admin/add-customer.html'" value="Add Customer"/> 

</div> 
</div> 
</section> 

试过代码

的源代码

执行上面的代码后,得到这个错误。

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='userform']/section[1]/div/div[2]/input"}

Command duration or timeout: 32 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.46.0', revision: '87c69e2', enter code here time: '2015-06-04 16:17:10' System info: host: 'SKETCH_QA-02', ip: '10.70.1.32', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_20' Driver info: org.openqa.selenium.firefox.FirefoxDriver

+0

这是因为你的XPath是可怕试试下面这code。在这里提出这样的问题之前,你应该多了解一下它。 – acikojevic

回答

2

使用xpath locator

driver.findElement(By.xpath("//input[@value='Add Customer']")).click(); 

的XPath的说明试试这个下面code: -使用value<input>标签的属性。

注意: -而不是使用absolute xpath,请使用relative xpath

OR

使用cssSelector

driver.findElement(By.cssSelector("input[value='Add Customer']")).click(); 
+0

你有试过这个代码吗? –

+0

嗨耆那教,谢谢你的回答。我已经通过添加driver.manage()。timeouts()来解决这个问题了。implicitlyWait(10,TimeUnit.SECONDS);之前的代码。现在它工作正常:) –

+0

请'接受'这个答案,所以它会帮助其他用户以及:)并且你不明白我的观点,你已经输入了绝对路径,这不是很好的做法。因为一旦你的html会被修改,你会得到一个像'element not found'这样的错误,所以你最好使用相对xpath。 –

相关问题