2017-03-09 175 views
0

人體局部圖像我使用driver.findElement(By.xpath(".//*[@id='bodyPartContainer']/div[1]")).click();選擇人體圖像的一部分,我得到異常下面的webdriver無法找到元素使用XPath

org.openqa.selenium.NoSuchElementException:無這樣的元素:無法找到元素:{ 「方法」: 「的xpath」, 「選擇器」: 「.//*[@ 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 = "[email protected]"; 
            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

你使用了哪些硒結合物? – ExperimentsWithCode

+0

可能還想嘗試檢查包含文本的humanBodyPart類。 http://stackoverflow.com/questions/37408657/selenium-find-element-by-visible-text – ExperimentsWithCode

+0

我使用硒webdriver自動化我的測試在java春季mvc和javascript,jquery,html5,css3爲前端。 –

回答

2

我認爲這是因爲你缺少xpath中指定元素標記的部分。

而不是.//[@id='bodyPartContainer']/div[1]嘗試.//div[@id='bodyPartContainer']/div[1]

.//*[@id='bodyPartContainer']/div[1]也可以工作(如果你想通配元素類型)。

+0

我試過了,似乎沒有爲我工作。我得到同樣的例外。 –

0

您在xpath表達式中缺少標籤名稱(div)。

基本語法是:

driver.findElement(By.xpath(".//(tagName or else *)[@(attributename)='(attributevalue)']")) 

更正你的說法下面提到聲明

driver.findElement(By.xpath(".//div[@id='bodyPartContainer']/div[1]")).click(); 

如果你不想提標記名可以更換*標記名的就地

driver.findElement(By.xpath(".//*[@id='bodyPartContainer']/div[1]")).click(); 
+0

我嘗試了這兩種方法,並得到相同的異常org.openqa.selenium.NoSuchElementException:no such element:Unable to locate element:{「method」:「xpath」,「selector」:「.//*[@ ID = 'bodyPartContainer']/DIV [1]「} –