Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Xpath昨天工作得很好,但今天不工作了_Java_Xpath_Selenium Webdriver - Fatal编程技术网

Java Xpath昨天工作得很好,但今天不工作了

Java Xpath昨天工作得很好,但今天不工作了,java,xpath,selenium-webdriver,Java,Xpath,Selenium Webdriver,Xpath昨天对于我想要单击的按钮运行良好。但它今天不起作用。当尝试运行脚本时,不会出现任何TouchElementException。下面我添加了代码和错误 HTML代码: “添加客户”按钮的源代码 <section class="content-header"> <div class="row"> <div class="col-xs-6"> <div class="col-xs-6 text-right"> <a class="btn

Xpath昨天对于我想要单击的按钮运行良好。但它今天不起作用。当尝试运行脚本时,不会出现任何TouchElementException。下面我添加了代码和错误

HTML代码:

“添加客户”按钮的源代码

<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>
错误:

执行上述代码后,获取此错误

线程主org.openqa.selenium.NoSuchElementException中的异常: 找不到元素: {方法:xpath,选择器:./*[@id='userform']/section[1]/div/div[2]/input}

命令持续时间或超时:32毫秒 有关此错误的文档,请访问: 构建信息:版本:“2.46.0”,修订版:“87c69e2”,在此输入代码时间:“2015-06-04 16:17:10” 系统信息:主机:'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' 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver

使用xpath定位器尝试下面的代码

xpath说明:-使用标记的value属性

注意:-使用相对xpath而不是绝对xpath

使用cssSelector尝试下面的代码


这是因为您的xpath很糟糕。在这里问这些问题之前,你应该了解更多。嗨,杰尼什,谢谢你的回答。我已经通过添加driver.manage.timeouts.implicitlyWait10,TimeUnit.SECONDS解决了这个问题;在代码之前。现在它工作正常了:请接受这个答案,这样对其他用户也会有帮助:而且您不理解我的观点,您输入了绝对xpath,这不是一个好的实践。因为一旦修改html,就会出现类似“未找到元素”的错误,所以最好使用相对xpath。
//Clicking on 'Add Customer' button

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