Home > Net >  Jmeter´s WebDriver Sampler - CSS Locator Wildcard
Jmeter´s WebDriver Sampler - CSS Locator Wildcard

Time:04-07

I´ve been doing some web scrapping using Jmeter´s built-in WebDriver Sampler with Selenium.

Context: At a certain point, i need to select ONLY the pre-selected date that comes out when "opening" a datepicker (that date is always the actual date). Here is a pic showing how it looks like:

Date Picker

As you can notice, the pre-selected date is 4/4/2022 (today´s date), and the CSS Locator of that specific cell is #_z_0-w1 > td.z-calendar-cell.z-calendar-weekday.z-calendar-selected

I´ve tried different ways to aproach this:

  1. I tried parsing and passing sysdate (with sendKeys()) using Jmeter´s __time function but it appears that the specific front-end field i´m interacting with expects you to choose from the date picker.

  2. I also tried using Xpath to select a sepecific cell from the date picker´s calendar but what i need is to ALWAYS choose the pre-selected date that appears when you open the datepicker.

  3. So what i ended up doing is locating the pre-selected date picker´s cell using Selenium´s CSS selector as it follows:

//WDS.browser.findElement(pkg.By.cssSelector("#_z_0-w3 > td.z-calendar-cell.z-calendar-weekday.z-calendar-selected")).click();

Issue: This worked perfectly fine, but i found out that when the month´s week number changes (for example, from 1, to 2) my script ends up failing because the css selector I´m using specifies the week´s number (#_z_0-w1 > .....).

In order to solve this issue i need to implement some sort of wildcard character that allows me to always select the cell with that specific css locator regardless of the month´s week number. I´ve been doing my reaserch and tried with different lines of code but i can´t manage to make it work (it always ends up saying that i have a syntax issue or that it can´t find the corresponding web element).

Any sort of help that i could get would be really aprecciated. Also, sorry for my rusty english, it´s not my mother tongue.

CodePudding user response:

I don't think CSS Selectors support wildcards in the attributes names

The easiest option is calculating the current week number beforehand and use string concatenation to insert it into the CSS expression, something like:

var currentWeek = Math.floor(new Date().getDate() / 7)   1
WDS.browser.findElement(pkg.By.cssSelector("#_z_0-w"   currentWeek > td.z-calendar-cell.z-calendar-weekday.z-calendar-selected")).click()

More information on the WebDriver Sampler: The WebDriver Sampler: Your Top 10 Questions Answered

  • Related