如何解决测试中Xpath的invalidselectoreexception ?



code:-

公共类carserviceinfpage {public Object[][] carserviceinfpage (WebDriver driver,ExtentTest test,ExtentReports extent)抛出IOException {

int flag = 0;
String b = "";
int rowCount = 1;


XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("CarService CenterData");

Object[][] obj = new Object[20][3];
obj[0][0] = "Name";
obj[0][1] = "Address";
obj[0][2] = "PhoneNumber";


Object[][] output = new Object[10][3];
int outputrow = 0; 

try {
List<WebElement> elements = driver.findElements(By.className("store-details"));
@SuppressWarnings("unused")
String[] phone = null;
String phoneString = null;
String[] stringArray = new String[0];
int j = 0;
float rat[]=new float[elements.size()]; 
for(int i=0;i<elements.size();i++) {
String temp =elements.get(i).findElement(By.className("green-box")).getText();
rat[i]= Float.parseFloat(temp);
String Vote = elements.get(i).findElement(By.xpath("//*[@id="bcard"+i+""]/div[1]/section/div[1]/p[1]/a/span[3]")).getText();
String numberOnly= Vote.replaceAll("[^0-9]", "");
int VoteInteger = Integer.parseInt(numberOnly);
List<WebElement> PhoneNumberString =  driver.findElements(By.className("mobilesv"));
if(flag == 0) {
for(int k = 0;k<PhoneNumberString.size();k++) {
phoneString = PhoneNumberString.get(k).getAttribute("class").split("-")[1];
MobileClass r = new MobileClass();
String temp2 = r.mobileClass(phoneString);
b += temp2;
}
flag = 1;
while(j<b.length()) {
if(b.charAt(j) == '0') {
int count = 0;  
String var = "";
while(j<b.length() && count <= 10) {
var += b.charAt(j); 
j++;count++;
}
stringArray = Arrays.copyOf(stringArray,stringArray.length+1);
stringArray[stringArray.length - 1] = var;
}
else if(b.charAt(j) == '+') {
int count = 0;
String var = "";
while(j<b.length() && count <= 15) {
var += b.charAt(j); 
j++;count++;
}
stringArray = Arrays.copyOf(stringArray,stringArray.length+1);
stringArray[stringArray.length - 1] = var;
}
}
}
if(rat[i]>=4.0 && VoteInteger > 20) {
output[outputrow][0] = elements.get(i).findElement(By.className("lng_cont_name")).getText();
obj[rowCount][0] = elements.get(i).findElement(By.className("lng_cont_name")).getText();

output[outputrow][1] = elements.get(i).findElement(By.className("cont_sw_addr")).getText();
obj[rowCount][1] = elements.get(i).findElement(By.className("cont_sw_addr")).getText();

output[outputrow][2] = stringArray[i];
obj[rowCount][2] = stringArray[i];
rowCount++;
}
outputrow++;
}

test = extent.createTest("Car Service Info Searching");

test.log(Status.INFO, "This step shows usage of log,info");
test.info("This test shows searching car wash service and printing on Console");
test.pass("Passed",MediaEntityBuilder.createScreenCaptureFromPath("screenshot3.png").build());

} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
test.fail("Failed",MediaEntityBuilder.createScreenCaptureFromPath("screenshot3.png").build());
}
int rows = obj.length;
int cols = obj[0].length;


for(int k=0;k<rows;k++) {
XSSFRow row = sheet.createRow(k);
for(int p=0;p<cols;p++) {
XSSFCell cell = row.createCell(p);
Object value = obj[k][p];
if(value instanceof String) {
cell.setCellValue((String)value);
}
if(value instanceof Integer) {
cell.setCellValue((Integer)value);
}
}
}


String filePath = "./ExcelReport/JustDialService.xlsx";
FileOutputStream outstream = new FileOutputStream(filePath);
workbook.write(outstream);
outstream.close();
workbook.close();

return output;
}

}

错误:-

org.openqa.selenium。InvalidSelectorException:无效的选择器:无法找到具有xpath表达式的元素//[@id='bcard'0']/div[1]/section/div[1]/p[1]/a/span[3],因为以下错误:SyntaxError: Failed to execute 'evaluate' on 'Document':字符串'//[@id='bcard'0']/div[1]/section/div[1]/p[1]/a/span[3]'不是有效的XPath表达式。(会话信息:chrome=101.0.4951.67)有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/invalid_selector_exception.html构建信息:版本号:'3.141.59',修订号:'e82be7d358',时间:'2018-11-14T08:17:03'java。版本:"1.8.0_181"驱动程序信息:org.openqa.selenium.chrome.ChromeDriverCapabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 101.0.4951.67, chrome: {chromedriverVersion: 101.0.4951.41 (93c720db8323…, userDataDir: c: users welcome appdata lo…}, goog:chromeOptions: {debuggerAddress: localhost:53349}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: proxy (), setWindowRect: true, strictFileInteractability: false,超时:{implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior:辞退和通知,webauthn:扩展:credBlob: true, webauthn:扩展:largeBlob: true, webauthn:virtualAuthenticators: true}会话ID: afe3a97929dbe540e60ed0c502834e77* * *元素信息:{使用= xpath值=//* [@ id ="bcard ' 0 ']/div[1]/部分/div [1]/p[1]//跨度[3]}

Trythisxpath

"//*[@id='bcard'"+i+"]/div[1]/section/div[1]/p[1]/a/span[3]"

不是

"//*[@id="bcard"+i+""]/div[1]/section/div[1]/p[1]/a/span[3]"

在下面的行

String Vote = elements.get(i).findElement(By.xpath("//*[@id="bcard"+i+""]/div[1]/section/div[1]/p[1]/a/span[3]")).getText();

最新更新