我必須從字串中減去 -1PR001-CC001578并將其作為引數傳遞xpath給 來標識一個元素。我將它拆分CC并從中減去 -1 001578。結果是1577。但是前導零被洗掉,因此xpath識別失敗。
let courseID = "PR001-CC001578";
let currCourseID = courseID.split('CC');
let otherCourseID = currCourseID[1]-1;
console.info("other Course ID:", otherCourseID);
var courseIDAssetsPg="//div[contains(text(),'%d')]";
var replaceCCId = courseIDAssetsPg.replace("%d", otherCourseID);
var CCIdLoc = element(by.xpath(replaceCCId));
console.info("locator: ", CCIdLoc )
輸出:
other Course ID: 1577 //missing 0's here
locator : //div[contains(text(),'1577')]
請讓我知道有沒有其他方法可以處理這個問題。我希望定位器是//div[contains(text(),'PR001-001577')]
提前致謝 !
uj5u.com熱心網友回復:
我想另一種方法是使用這種方法將 id 分成兩部分,以這種方式更改數字并根據 6 位格式恢復結果編號:
let courseID = "PR001-CC001578";
const parts = courseID.split('-');
const lastNumber = parts[1].replace(/\D/g, "") - 1;
const formattedLastNumber = `${lastNumber}`.padStart(6, '0');
console.log(formattedLastNumber);
uj5u.com熱心網友回復:
作為中間步驟,您可以使用正則運算式來查找和提取前導零,將它們保存到一個附加變數(可選)中,并在您完成數學運算后將它們添加到新數字中。
但是,您必須考慮特殊情況,即數學運算后前導零的數量發生變化(例如,1000-1=999)。
let courseID = "PR001-CC001578";
let currCourseID = courseID.split('CC');
let leadingZeros = currCourseID[1].match(/^0*/); // changed this
let otherCourseID = leadingZeros (currCourseID[1] - 1); // and changed this
if (otherCourseID.length < currCourseID[1].length) {
otherCourseID = "0" otherCourseID;
}
console.info("other Course ID:", otherCourseID);
var courseIDAssetsPg="//div[contains(text(),'%d')]";
var replaceCCId = courseIDAssetsPg.replace("%d", otherCourseID);
var CCIdLoc = element(by.xpath(replaceCCId));
console.info("locator: ", CCIdLoc )
或者,您可以簡單地用適當數量的前導零填充數字:
const numZeros = currCourseID[1].length - otherCourseID.toString().length;
otherCourseID = "0".repeat(numZeros) otherCourseID;
uj5u.com熱心網友回復:
我認為最簡單的方法是使用 RegEx 決議數字,根據需要使用該數字(加或減 1),然后通過添加足夠的前導零來組合一個新的 6 位數字,然后將其插入到您的字串中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366541.html
標籤:javascript 爪哇 硒
上一篇:硒串列框按底部排序
下一篇:selenium.common.exceptions.ElementNotInteractableException:訊息:切換框架后元素不可互動
