c# - 使用 LinkText 时可以使用字符串变量吗?
问题描述
在 Visual Studio 中使用 Selenium。我正在尝试单击列表中的项目。该项目具有唯一的 ID。
CA-41107005-00000040
我不想引用实际的 ID 号,而是希望通过引用将存储项目 ID 的字符串变量来使测试更加动态。我称这个变量为:changeActionNumber
我试过点击这样的项目:
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时不能使用变量吗?
解决方案
试试下面xapth
wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//a[text()='" +changeActionNumber + ']"))).Click();
或者
wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//a[contains(.,'" +changeActionNumber + ')]"))).Click();
推荐阅读
- c# - 如何在 WinForms 中使用来自服务器的外部配置文件?
- ejs - 在我的编码课程中使用 EJS 时出现编译器错误
- html - 强制图像调整大小以适合 div
- assembly - 为什么汇编中字符串变量的末尾包含 0?
- kubernetes - 从公共 IP 访问 Minikube 服务
- raster - 结合 2 个具有不同范围、分辨率和点规律性的栅格数据集
- cmake - 为什么我不能更新到最新版本的 libtorch?
- javascript - 如何将预构建的 next.js 应用程序部署到 Vercel(没有 Git 集成)?
- flutter - Flutter:如何检测在 TextField 上按下了删除键
- neo4j - 密码从结果中删除 null s