2017-03-05 30 views
0

看來X路徑存在一些可怕的錯誤。我想連接循環內的X路徑,但結果是錯誤消息。 請幫我出這個Selenium Xpath組合

WebElement e=driver.findElement(By.xpath("//tr[2]//tr//table/tbody/tr[1]/td/table[contains(@id,'fs')]/tbody")); 

      for (int i=1; i<numRows; i++) 
      { 
        Row row = sheet.getRow(i); 
        if (row != null){ 
         for (int j=0; j<numCols; j++){ 
          if (row.getCell(j) != null){ 
           if(row.getCell(j).getStringCellValue() != null) { 

            if(j==0 && !details[i][j].equalsIgnoreCase("XXX")) 
            { 
            WebElement style=driver.findElement(By.xpath(e+"/tr["+k+"]/td[1]/input")); 
            style.sendKeys(details[i][j]); 
            style.sendKeys(Keys.TAB); 
            ReusableMethods.expliwait(driver); 
            } 

的顯示在Eclipse控制檯 org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression [[ChromeDriver: chrome on XP (51c2231ca09b3dab440d7c6ebce322de)] -> xpath: //tr[2]//tr//table/tbody/tr[1]/td/table[contains(@id,'fs')]/tbody]/tr[0]/td[1]/input

下面的錯誤消息,我已經標註在上面的結果以粗體顯示的錯誤表達。我不知道爲什麼「]」將被加入,而串聯的x路

回答

1

如果你想與XPath//tr[2]//tr//table/tbody/tr[1]/td/table[contains(@id,'fs')]/tbody + /tr["+k+"]/td[1]/input您可能需要使用下面的語法搭配元素:

WebElement e=driver.findElement(By.xpath("//tr[2]//tr//table/tbody/tr[1]/td/table[contains(@id,'fs')]/tbody")); 
WebElement style=e.findElement(By.xpath("./tr["+k+"]/td[1]/input")); 

而且我建議你避免這種XPath和使用simple relative XPath表達式代替

+0

謝謝安德森!我不能使用相關的ID作爲繼續更改.. – Abhishek

+0

可以使用相對'XPath'而不涉及'id'。或者如果你想像'一樣動態地改變'id',你可以嘗試像'// tag [contains(@id,「constant_part _」)]' – Andersson