0

i am tying to use findelements to get all the text into one object and then see if a string is present in the list or not

Browser_Launch("chrome"); sign_in(); String a = null; Element_to_be_clicked("xpath", "//*[@id='history-guide-item']/a"); List<WebElement> text = driver.findElements(By.xpath("//ol[@class='item-section']//li//div//a")); for (int i = 0; i < text.size(); i++) { a = text.get(i).toString(); System.out.println(a); } if (a.contains("Will & Grace - Back This Fall")) { System.out.println("test passed"); } else { System.out.println("test failed"); } 

Website: Youtube page: history

test: trying to get all the name of the videos present in history page to a object so that i can assert

Thank you

3 Answers 3

1

Firstly, you need to declare a as list of string like: List<String> a = new ArrayList<>();

Secondly, since text is a List<WebElement> you should get the text of the web element and then compare the getText() of the link of anchor tag.

So instead of a = text.get(i).toString(); it should be a.add(text.get(i).getText());. This way we are adding all video name to a list.

Updated code:

 Browser_Launch("chrome"); sign_in(); List<String> a = new ArrayList<>(); Element_to_be_clicked("xpath", "//*[@id='history-guide-item']/a"); List<WebElement> text = driver.findElements(By.xpath("//ol[@class='item-section']//li//div//a")); for (int i = 0; i < text.size(); i++) { a.add(text.get(i).getText()); System.out.println(text.get(i).getText()); } if (a.contains("Will & Grace - Back This Fall")) { System.out.println("test passed"); } else { System.out.println("test failed"); } 
Sign up to request clarification or add additional context in comments.

Comments

0

You are doing nothing inside the loop, try this:

Browser_Launch("chrome"); String textToFind = "Will & Grace - Back This Fall"; boolean isTextPresent = false; sign_in(); Element_to_be_clicked("xpath", "//*[@id='history-guide-item']/a"); List<WebElement> text = driver.findElements(By.xpath("//ol[@class='item-section']//li//div//a")); for (int i = 0; i < text.size(); i++) { if (text.get(i).toString().contains(textToFind)) { isTextPresent = true; break; } } if (isTextPresent) { System.out.println("test passed"); } else { System.out.println("test failed"); } 

1 Comment

Thanks for reply but List<WebElement> text = driver.findElements(By.xpath("//ol[@class='item-section']//li//div//a")); it is storing the Xpath in side the text Object not the actual text
0

This xpath (//ol[@class='item-section']//li//div//a) is wrong.

change it with //a[@dir='ltr']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.