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

C# Selenium点击class为bi bi-arrow-right-square-fill的元素失败求助

解决方法

首先你写的CSS选择器存在语法错误,多个类名组合的正确写法是用.直接连接,不能留空格也不能添加<>,错误的.&lt;bi bi-arrow-right-square-fill&gt;要改成.bi.bi-arrow-right-square-fill。

另外页面里有两个class为bi bi-arrow-right-square-fill的<i>元素——一个嵌套在<a>标签内,一个是独立存在的,你需要明确要点击哪一个,否则可能定位到错误元素导致点击无效。

正确的实现代码

1. 定位并点击<a>标签内的箭头元素

// 等待元素加载完成并处于可交互状态
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement linkArrow = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("a > .bi.bi-arrow-right-square-fill")));

// 先尝试正常点击,若被遮挡再用JS点击
try
{
    linkArrow.Click();
}
catch (ElementClickInterceptedException)
{
    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
    executor.ExecuteScript("arguments[0].click();", linkArrow);
}

2. 定位并点击独立的箭头元素

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement standaloneArrow = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("i.bi.bi-arrow-right-square-fill:not(a > i)")));

try
{
    standaloneArrow.Click();
}
catch (ElementClickInterceptedException)
{
    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
    executor.ExecuteScript("arguments[0].click();", standaloneArrow);
}

关键注意点

  • 必须等待元素处于可交互状态再执行点击操作,避免因元素未加载完成导致的定位失败
  • 多个类名的CSS选择器要使用.class1.class2格式,空格代表后代选择器,会匹配所有后代元素而非当前元素同时拥有两个类
  • 区分两个相同类的元素时,可通过父元素特征缩小定位范围,确保定位到目标元素

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:25:10