我正在使用driver.findElement(By.xpath(".//*[@id='bodyPartContainer']/div[1]")).click();
来选择人体图像的一部分,并在webdriver中获得以下异常
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“.//* [@ id ='bodyPartContainer'] / div [1 ]“}
<head>
<body style="height: 100%">
<div class="container-fluid">
<div class="row">
<!-- Name-->
<div class="row rowTop">
<!-- Email -->
<div class="row rowTop">
<!-- Date of Birth-->
<div class="row rowTop">
<!-- Human Body Selection -->
<form id="vForm" class="form-horizontal" role="form" data-toggle="validator" novalidate="true">
<!-- Type of Request -->
<div class="row rowTop">
<!-- Description-->
<div class="row rowTop">
<!-- Human Body -->
<div class="row">
<div class="col-md-6">
<div class="col-md-3">
<div id="bodyPartContainer" style="margin-top: 30px; display: block;">
<div class="humanBodyPart">Head</div>
<div class="humanBodyPart">Eyes</div>
<div class="humanBodyPart">Nose</div>
<div class="humanBodyPart">Mouth</div>
<div class="humanBodyPart">Chin and Jaw</div>
<div class="humanBodyPart">Ears</div>
</div>
</div>
<div class="col-md-3">
</div>
<!-- Files -->
<div class="row" style="margin-top:10px">
<!-- File -->
<div class="row" style="margin-top:20px">
<!-- checkBox 1 -->
<div class="row rowTop">
<!-- checkBox 2 -->
<div class="row rowTop">
<!-- Submit Btn -->
<div class="row rowTop" style="margin-top:20px; margin-bottom:30px">
</form>
</div>
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<script type="text/javascript">
$(init);
pageUtil.url = "http://52.163.121.157:8080/Curis/";
var box = new BlackBox();
var editor;
var userName = "test 2";
var userEmail = "sidsri133196@gmail.com";
var userDob = "2017-02-09";
var selectedFiles = [];
function initHumanBodySelector() {
var list = [{}, {}, {}] $("#symptomContainer").empty();
$("#bodyPartContainer").empty();
$("#humanBody").empty();
$("#humanBody").humanBodySelector({
gender: $("#selectGender").val(),
callback: null,
basePath: pageUtil.url,
symptomContainer: $("#symptomContainer"),
bodyPartContainer: $("#bodyPartContainer")
});
}
function g_AjxUploadImg(btnImg) {
var button = btnImg,
interval, fileType;
new AjaxUpload(button, {
type: "POST",
action: getDataFromBack("getFsAdd", null, "file"),
name: 'upload[]',
data: {
cmd: 'upload',
target: 'C_',
nameAppendix: getTimeString()
},
datatype: 'JSON',
onChange: function(file, ext) {
fileType = ext;
},
onSubmit: function(file, ext) {
if (!(ext && /^(jpg|JPG|png|PNG|gif|GIF|pdf|PDF|doc|DOC|docx|DOCX)$/.test(ext))) { //alert(ext); return false; } }, onComplete:function(file, response) { var result=parseJson(response); debug(result); var fileHash=result.added[0].hash; var fileName=result.added[0].name; var param={}; param.fileHash=fileHash; fileAddress=getDataFromBack("generateFileAddressFromHash",param,"file"); $("#fileDownload").append('' +' <div class="fileBlk" id="file'+fileHash+'">' +'<i class="fa fa-file-o" style="font-size:45px;margin-top:13px"></i>' +'<h6 style="margin-top:4px">'+fileName+'</h6>' +'</div>'); $("#file"+fileHash).click(function(){ $("#fileViewFrame").attr("src",fileAddress); $("#myModal").modal("show"); }); selectedFiles.push(fileHash); //alert(url); //alert(url); }, error:function(data) { alert(data.status); } } ); } function getFileDetailsByFileHashArr(fileHashArr){ var fileDetailArr=[]; if(!isVarValid(fileHashArr)||!(fileHashArr instanceof Array)||fileHashArr.length==0){ return fileDetailArr; } for(var index in fileHashArr){ var param={}; param.fileHash=fileHashArr[index]; var fileDetail=getDataFromBack("getFileDetailByHash",param,"file"); fileDetailArr.push(parseJson(fileDetail)); } return fileDetailArr; } function init(){ $("#name").text(userName); $("#email").text(userEmail); $("#dob").text(userDob); $("#titleLabel").text("Request Appointment Form"); //$("#submit").bind("click",submit); $("#checkBox1").bind("click",checkBox1); $("#checkBox2").bind("click",checkBox2); $('#vForm').validator(); g_AjxUploadImg($("#file")); $('#vForm').validator().on('submit', function (e) { if (e.isDefaultPrevented()) { // handle the invalid form... debug("prevent!!!"); } else { // everything looks good! debug("good!"); submit(); e.preventDefault(); return false; } }); initHumanBodySelector(); } function checkBox1(){ var checkBox1=$("#checkBox1").val(); if(checkBox1==0){ $("#checkBox1").val("1"); }else{ $("#checkBox1").val("0"); } } function checkBox2(){ var checkBox2=$("#checkBox2").val(); if(checkBox2==0){ $("#checkBox2").val("1"); }else{ $("#checkBox2").val("0"); } } function submit(){ var checkBox1=$("#checkBox1").val(); var checkBox2=$("#checkBox2").val(); if(checkBox1==0||checkBox2==0){ box.alert("You must be agree with our conditions",emptyFunc); }else{ var param={}; param.type=$("#selectAppType").val(); param.description=$("#description").val(); param.conditions=json2String(healthConditions); param.fileHashArr=json2String(selectedFiles); box.load("submit",emptyFunc); asyncDataFromBack(afterSubmit,"applyForAppointment",param,"appointment") } } function afterSubmit(data, status){ box.ready("submit"); if(status==ReturnData.success){ box.alert("Success!", function(){ parent.frameGoTo("index/toIndex.action"); }); }else{ box.alert("Error Occured: "+data, emptyFunc); } } function back(){ goTo("appointment/makeAppointment.action"); }
</script>
<canvas id="firebugCanvas" class="firebugResetStyles firebugBlockBackgroundColor firebugCanvas" width="1366" height="440" style="display: none ! important;" />
<div class="mapster_tooltip" style="border: 2px solid black; background: rgb(238, 238, 238) none repeat scroll 0% 0%; width: 160px; padding: 4px; margin: 4px; box-shadow: 3px 3px 5px rgb(83, 83, 83); border-radius: 6px; opacity: 0.9; display: block; position: absolute; left: 198px; top: 693.6px; z-index: 9999;" data-opacity="0.9">Abdomen</div>
<div class="firebugResetStyles firebugBlockBackgroundColor" style="left: 689.5px ! important; top: 35.6px ! important; width: 307.25px ! important; height: 46px ! important; border-radius: 18px ! important; box-shadow: 0px 0px 2px 2px highlight ! important;">
</body>
</html>
任何人都可以帮助我。感谢。
答案 0 :(得分:2)
我认为这是因为您缺少特定元素标记的xpath部分。
而不是.//[@id='bodyPartContainer']/div[1]
尝试.//div[@id='bodyPartContainer']/div[1]
。
.//*[@id='bodyPartContainer']/div[1]
也可以工作(如果你想要通配符元素类型)。
答案 1 :(得分:0)
您在xpath表达式中缺少标记名称(div)。
基本语法是:
driver.findElement(By.xpath(".//(tagName or else *)[@(attributename)='(attributevalue)']"))
将您的陈述改为下述陈述
driver.findElement(By.xpath(".//div[@id='bodyPartContainer']/div[1]")).click();
如果您不想提及标记名,可以替换* inplace of tagname
driver.findElement(By.xpath(".//*[@id='bodyPartContainer']/div[1]")).click();