'I got the text from a dropdown and stored in a string and Im trying to locate an element with the text I got

The xpath to locate element is

//div[@class='modal-body']//div//span[starts-with(text(),'Bar/lounge')]

Bar/lounge is the text I got from a dropdown, I will just call the string I stored in that texts place.The problem is now I want bar alone .same case for the below texts these are the inputs I got through data provider from the dropdown.Is there way to satisfy in the xpath itself . [The texts are :

Conference space,
Swimming pool,
Free airport transportation,
Free WiFi,
Breakfast service,
Business services,
Bar/lounge,
Restaurant,
Fitness facilities]


Solution 1:[1]

You can find all the elements that contain a certain text. Then, select the one you need and click it:

List<WebElement> myElements = driver.findElementsByXpath("//*[contains(text(),'Bar')]") 
// this maybe another index, here is just an example
WebElement barElm = myElements[1];
barElm.click();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tal Angel