如何在自动化中查找类似于getText()的元素定位器

时间:2019-02-25 09:16:07

标签: java selenium mobile automation appium

如何在自动化中查找类似于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 个答案:

答案 0 :(得分:0)

因为没有发布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;
             }
            }