How to recover the second text in a validation with Assertequals

Asked

Viewed 152 times

0

I have that code:

inserir a descrição da imagem aqui

And I’m trying to retrieve the text "Test Automation".

then I made the following code.

WebElement idMenu = navegador.findElement(By.id("dropdown-menu-profile"));
String valortesteAutomacao = idMenu.getText();
assertEquals("Teste_Automacao", valortesteAutomacao);

but only appears to me:

inserir a descrição da imagem aqui

I tried for Xphat, but it does not return....

How to do?

2 answers

1

Your search is incorrect, by capturing by the id dropdown-menu-profile you are taking the entire text from the div. To only get the paragraph text you would need to be more specific, taking the child elements from the div or using xpath. Example of xpath:

//div[@id='dropdown-menu-profile']//p
  • So I did it this way too, but the "valueAuthored" is empty. How can I do it? I tried to get the daughter DIV too, but I can’t recover the text I need. Only what’s inside her.

  • to get all child elements you can do the following: List<Webelement> children = idMenu.findElements(By.xpath(".//*");&#Xa if you have doubts about what your xpath is returning, you can give a Ctrl+f in the Elements tab of the Chrome developer console and test there to see what your xpath is picking up

0


managed to make:

The following is, in case, I had to navigate between all the Parents, to get to what I wanted. So I used 3 types of By (id, classname, tagname). I just didn’t know about the tagname, which was what was missing before. So I didn’t have to use xPhat.

WebElement testeAutomacao = navegador.findElement(By.id("dropdown-menu-profile"));
WebElement idMenu = testeAutomacao.findElement(By.className("user-data"));
String valortesteAutomacao = idMenu.findElement(By.tagName("p")).getText();
assertEquals("Teste_Automacao", valortesteAutomacao);
idMenu.click();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.