简体   繁体   English

如何通过 class 和文本的一部分来 select 一个元素?

[英]How to select an element by part of class and text?

For a calendar, I have it setup as this:对于日历,我将其设置为:

<div class="Calendars__day___1LZkL">1</div>
<div class="Calendars__day___1LZkL">2</div>
<div class="Calendars__day___1LZkL">3</div>
<div class="Calendars__day___1LZkL">4</div>

and so on.等等。 I want to select the calendar day by class name and text but can't figure out how to get it to work.我想通过 class 名称和文本 select 日历日,但不知道如何让它工作。

I currently have a function setup like this:我目前有这样的 function 设置:

    calendarDay(day) {
        return $(`[class^="Calendars__day"][text()="${day}"]`);
    }

but this throws an error "invalid selector: invalid selector: An invalid or illegal selector was specified"但这会引发错误“无效的选择器:无效的选择器:指定了无效或非法的选择器”

I'm using WDIO to run automation and that is what the selector is for我正在使用 WDIO 来运行自动化,这就是选择器的用途

Figured it out, the selector to use that works is:弄清楚了,要使用的选择器是:

$(`//div[contains(@class, "Calendars__day") and contains(., ${day})]`);

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

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