Not able to get text atttibute for element

Hello there , I am using follwing line of code to fetch all product name .
List prod_list=driver.findElements(By.xpath("//android.widget.TextView[contains(@resource-id,‘productTitle’)]"));
for (WebElement prolst:prod_list)
{
System.out.println("Name of Product "+prolst.getAttribute(“text”));
}

But its provide me only text for visible element not of that , which are visible when scroll down.

you cannot get any elements outside screen with Android. you need scroll ALL to check ALL.

I tried that , I tried to scroll till end and than tried to get all text value , but its still gives me only that which is visible on screen.

you need in loop:

  1. get first item name
  2. get all items
  3. check all items names
  4. scroll down
  5. get new first item name
  6. check new first item name <> old first item name
    6.1) name different -> set old name to new name and go to step 2
    6.2) loop completed :slight_smile:

Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
System.out.println(“s=”+scrollStart);
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
for (int i = 0; i < dimensions.getHeight(); i++)
{
driver.swipe(0,scrollStart,0,scrollEnd,2000);
lst=driver.findElements(By.xpath(“//android.widget.TextView[contains(@resource-id,‘productTitle’)]”));
Thread.sleep(2000);
for(WebElement lst_pro:lst)
{

    	 String name=lst_pro.getAttribute("text");
    	 if(lst_pro.getAttribute("text").contains(name))
    	 {
    		 System.out.println("Duplicate");
    	 }
    	 else
    	 {
    		 list.add(name);
    		 System.out.println("Name of Product "+name);
    	 }
     }
     
     }

ALL i mean just do scroll and check, do next scroll and check again and until you reach end of scroll.

And your loop not correct. you need use something like “While” and compare did first or last visible element changed to detect end of scroll.

I tried but I am getting only right left text value and duplicate.

Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
System.out.println(“s=”+scrollStart);
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
for (int i = 0; i < dimensions.getHeight(); i++)
{
driver.swipe(0,scrollStart,0,scrollEnd,2000);
lst=driver.findElements(By.xpath("//android.widget.TextView[contains(@resource-id,‘productTitle’)]"));
for(WebElement lst_pro:lst)
{

        System.out.println("Product Name "+lst_pro.getAttribute("text"));        	 
    }
     }

with 162 results you need scroll about 80 times to check all. and it will be many duplicate searches while executing this. but at the end you will get all your 162 results.

Can you provide me with psuedo code for same . I am not able to get it.

example how i collect all date values in search:

public List<String> getAllCellDateValues() {
        String currentDate = itemDate.get(0).getText();
        String newDate;
        List<String> result = new ArrayList<String>();

        do {
            for (MobileElement el : itemDate) {
                if (!result.contains(el)) {
                    result.add(el.getText());
                }
            }
            swipeList("u");
            newDate = itemDate.get(0).getText();
            if (newDate.equals(currentDate)) { // let's swipe again for sure
                swipeList("u");
                newDate = itemDate.get(0).getText();
                if (newDate.equals(currentDate)) { // give up
                    break;
                }
            }
            currentDate = newDate;
        } while (true);

        return result;
    }
1 Like

Thanks , I used following line of code to attain my task:

Dimension dimensions = driver.manage().window().getSize();
		Double screenHeightStart = dimensions.getHeight() * 0.5;
		int scrollStart = screenHeightStart.intValue();
		System.out.println("s="+scrollStart);
		Double screenHeightEnd = dimensions.getHeight() * 0.2;
		int scrollEnd = screenHeightEnd.intValue();
		ArrayList<String> result = new ArrayList<String>();
        for (int i = 0; i < dimensions.getHeight(); i++) 
		{
			driver.swipe(0,scrollStart,0,scrollEnd,0);
			lst=driver.findElements(By.id("com.snapdeal.main:id/productTitle"));
  			for(WebElement lst_pro:lst)
			{
				String pro_name=lst_pro.getAttribute("text");
				if(!result.contains(pro_name.trim()))
				{
					System.out.println("Name of Product :"+pro_name+"  Product fetched Till Now :"+result.size());
					result.add(pro_name.trim());
			
				}
			}
		}

Its work but , still wonder why it is not getting last element text value .

Thanks for your help.

try to print all you trying - maybe duplicate. try to stop test execution on last element and manually check in any Elements inspector what you see.