首页 > 解决方案 > 使用 LinkText 时可以使用字符串变量吗?

问题描述

在 Visual Studio 中使用 Selenium。我正在尝试单击列表中的项目。该项目具有唯一的 ID。

CA-41107005-00000040

我不想引用实际的 ID 号,而是希望通过引用将存储项目 ID 的字符串变量来使测试更加动态。我称这个变量为:changeActionNumber

该项目的 HTML 代码如下所示: 在此处输入图像描述

我试过点击这样的项目:

wait.Until(ExpectedConditions.ElementToBeClickable(By.LinkText($"{changeActionNumber}"))).Click();

也像这样:

wait.Until(ExpectedConditions.ElementToBeClickable(By.LinkText(changeActionNumber))).Click();

但是这两种情况都给出了相同的错误:

Message: OpenQA.Selenium.WebDriverTimeoutException : Timed out after 10 seconds
  ----> OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"link text","selector":"CA-41107005-00000040"}
  (Session info: chrome=77.0.3865.75)

使用LinkText时不能使用变量吗?

标签: c#stringvisual-studioseleniumlinktext

解决方案


试试下面xapth

wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//a[text()='" +changeActionNumber + ']"))).Click();

或者

wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//a[contains(.,'" +changeActionNumber + ')]"))).Click();

推荐阅读