Home > Back-end >  I want to make it possible : concat string and variables with locator in Selenium with java
I want to make it possible : concat string and variables with locator in Selenium with java

Time:08-26

Here is the task we have to resolve. Scenario : 1> Every time user opens the test, he must be asked to input date and month.

2> Those scanned inputs variables d1 and m1 called by method esi

3> id element with that month and date must be seen clicked on website in date section based on user input. The of id for all dates is something like :

Tag div , id = bkmgFlights_travelDates_1-date-2022-10-21 , in which start is always same for all dates as bkmgFlights_travelDates_1-date-2022-

So I have already done the 1> and 2> successfully, for the 3rd task, I am trying the below, but not working.

driver.findElement(By.xpath("//div[contains(@id,'bkmgFlights_travelDates_1-date-2022-')]" esi.m1 "-" esi.d1)).click();

I also tried action class, concat is happening as i can see colours and no errors, but still not working. It shows invalid selector, sometimes shows differet errors with action class, when I try

action1.click(driver.findElement(By.cssSelector("//div[contains(@id,'bkmgFlights_travelDates_1-date-2022-')]" esi.m1 "-" esi.d1)));

Please help me with how we can locate elements based on path concatenated string and variable and then at last performing click on it. Thank you. enter image description here

CodePudding user response:

You can try this:

string locator = String.format("//div[contains(@id,'bkmgFlights_travelDates_1-date-2022-%s-%s')]", esi.m1, esi.d1);
driver.findElement(By.xpath(locator)).click();

If you want to learn more about string interpolation from here, if you wish.

  • Related