简体   繁体   English

Jmeter 的 WebDriver 采样器 - CSS 定位器通配符

[英]Jmeter´s WebDriver Sampler - CSS Locator Wildcard

I´ve been doing some web scrapping using Jmeter´s built-in WebDriver Sampler with Selenium.我一直在使用 Jmeter 的内置 WebDriver Sampler 和 Selenium 进行一些 web 报废。

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).上下文:在某个时候,我只需要 select “打开”日期选择器时出现的预选日期(该日期始终是实际日期)。 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正如您所注意到的,预选日期是 4/4/2022(今天的日期),并且该特定单元格的 CSS 定位符是#_z_0-w1 > td.z-calendar-cell.z-calendar-weekday .z-日历选择

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.我尝试使用 Jmeter 的 __time function 解析和传递 sysdate(使用 sendKeys()),但看起来我正在与之交互的特定前端字段希望您从日期选择器中进行选择。

  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.我还尝试使用 Xpath 到 select 日期选择器日历中的特定单元格,但我需要的是始终选择打开日期选择器时出现的预选日期。

  3. So what i ended up doing is locating the pre-selected date picker´s cell using Selenium´s CSS selector as it follows:所以我最后做的是使用 Selenium 的 CSS 选择器定位预选日期选择器的单元格,如下所示:

//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 >.....).问题:这工作得很好,但我发现当月的周数发生变化时(例如,从 1 到 2)我的脚本最终失败,因为我使用的 css 选择器指定了周数( #_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.为了解决这个问题,我需要实现某种通配符,它允许我始终使用特定的 css 定位器 select 单元格,而不管月份的周数。 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).我一直在研究并尝试使用不同的代码行,但我无法使其正常工作(它总是以语法问题结束或找不到相应的 web 元素而告终)。

Any sort of help that i could get would be really aprecciated.我能得到的任何帮助都会非常感激。 Also, sorry for my rusty english, it´s not my mother tongue.另外,抱歉我生疏的英语,这不是我的母语。

I don't think CSS Selectors support wildcards in the attributes names我认为 CSS 选择器不支持属性名称中的通配符

The easiest option is calculating the current week number beforehand and use string concatenation to insert it into the CSS expression, something like:最简单的选择是预先计算当前周数并使用字符串连接将其插入到 CSS 表达式中,类似于:

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有关 WebDriver Sampler 的更多信息: WebDriver Sampler:回答的前 10 个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM