0

I have a problem because I do not know how to check or search if a text (String) is contained in a child element of a list. I have the following:

<div class="father_class">
  <div class="child">
    <div class="Elements">Profile</div>
  </div>
  <div class="child">
    <div class="Elements">Settings</div>
  </div>
</div>

I want to search on "father_class" if there is any "child" class which has the text Profile (as an String input). I have try with the following code (Cucumber on Java for Selenium), but it is not possible to include Profile as a String:

@FindBy (className="father_class")
private WebElementFacade Father
Father.containsElements(By.xpath("//div[@class='child']/div[contains(text(), 'Profile')]"));

But here the problem is that this 'Profile' is not entered as a String which is what I want:

@FindBy (className="father_class")
private WebElementFacade Father
String var = "Profile"
Father.containsElements(By.xpath("//div[@class='child']/div[contains(text(), var)]"));

Thank you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

Seems you were close enough. To pass a variable e.g. var within the you can use the following Locator Strategy:

@FindBy (className="father_class")
private WebElementFacade Father
String var = "Profile"
Father.containsElements(By.xpath("//div[@class='child']/div[contains(., '" +var+ "')]"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352