问题描述
如何在自动化中查找类似于getText()的元素定位器
[我正在测试应用程序模块,其中有从1到50的天标尺,每次我去该模块时,天数都会在上述范围内变化。 因此,如何获取元素值(ID,Xpath,链接等),以便检测到其元素,并在下面的循环中传递其值。 现在,我将硬核日值“ 38”存储在“ j”中。 但是我想对'j'使用动态值,以便将其减少到第1天]
[查找所附图像以供参考] [1]
for (int j = 38; j >= 1 ; j--) { System.out.println(j); if (j >= 2) { Thread.sleep(2000); By day38 = By.xpath("//*[@text='"+j+"']"); Thread.sleep(2000); objWrapperFunctions.mobileClick(day38); } else { Thread.sleep(2000); By day1 = By.xpath("//*[@text='1']"); Thread.sleep(2000); objWrapperFunctions.mobileClick(day1); break; } }
1楼
由于没有发布html,但是,
// Case 1=> If we assume you get dynamic day based on below logic
String dymamicDay=driver.findElement(By.id("someIDforDay")).getAttribute("value");
// get int for String read
int dDay =Integer.parseInt(dymamicDay);
// Pass dynamic value for j in loop
for (int j = dDay; j >= 1 ; j--)
{
System.out.println(j);
if (j >= 2) {
Thread.sleep(2000);
By day38 = By.xpath("//*[@text='"+j+"']");
Thread.sleep(2000);
objWrapperFunctions.mobileClick(day38);
} else {
Thread.sleep(2000);
By day1 = By.xpath("//*[@text='1']");
Thread.sleep(2000);
objWrapperFunctions.mobileClick(day1);
break;
}
}
// Case 2=> If we assume you get dynamic day based on below logic
List<WebElement> dynamicDay=driver.findElements(By.id("someIDforDay"));
// get no of days from List.size()
int dDay =dynamicDay.size();
// Pass dynamic value for j in loop
for (int j = dDay; j >= 1 ; j--)
{
System.out.println(j);
if (j >= 2) {
Thread.sleep(2000);
By day38 = By.xpath("//*[@text='"+j+"']");
Thread.sleep(2000);
objWrapperFunctions.mobileClick(day38);
} else {
Thread.sleep(2000);
By day1 = By.xpath("//*[@text='1']");
Thread.sleep(2000);
objWrapperFunctions.mobileClick(day1);
break;
}
}