You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebdriverIO如何点击特定类下文本为Course 12345的第二个超链接?

Hey there! I get it—dealing with duplicate element selectors can be tricky, especially when you need to target a specific one from a list. Let’s go through a few solid ways to click that second "Course 12345" link you’re after:

1. Use $$ to Get All Matches and Target by Index

The simplest approach is to fetch all elements matching your selector, then pick the second one (remember, arrays are zero-indexed, so we use [1]):

// Grab all links with text "Course 12345", then click the second one
$$('a=Course 12345')[1].click();

Just make sure to wait for the elements to load first if your page is dynamic—add a wait step to avoid errors:

const secondCourseLink = $$('a=Course 12345')[1];
secondCourseLink.waitForExist({ timeout: 5000 }); // Wait up to 5 seconds
secondCourseLink.click();

2. Filter Elements by Parent Context + Text

Since you mentioned elements are in a list with the .LO_field class, you can narrow down the scope first, then filter for your target text and pick the second match:

// Get all <a> tags inside .LO_field, filter for the ones with your text, then click the second
$$('.LO_field a').filter(async el => await el.getText() === 'Course 12345')[1].click();

This is great if you need to ensure you’re only looking within the specific list container, not the whole page.

3. Use XPath for Precise Positioning

XPath is super powerful for targeting elements by position. You can directly select the second <a> tag with your target text:

// Select the second <a> element with exact text "Course 12345"
$('//a[text()="Course 12345"][2]').click();

If you want to restrict this to only links inside .LO_field, adjust the XPath to:

$('//div[contains(@class, "LO_field")]//a[text()="Course 12345"][2]').click();

A Quick Note on Your Previous Attempt

Your earlier selector $('.LO_field [a=Course 12345]') didn’t work because that syntax is invalid—you can’t nest the text selector like that. The methods above fix that by either combining context with filtering or using XPath to target the right element.

Pick the method that fits your page structure best—if the links are directly in a list without extra nesting, the first approach is fastest. If you need to stay within a specific container, the filter or XPath method is more reliable.

内容的提问来源于stack exchange,提问作者javacripz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:51:36