如何单击< a>锚标签链接仅知道其硒C#的跨度

发布于 2025-02-11 08:21:52 字数 668 浏览 1 评论 0原文

在网页上,我想使用硒单击此锚点:

<a onclick='ejecutaOpcion(this, '/srDocumentoAdmRecepcion.do?accion=goInicioGet&estadoDoc=01&coDep=10016','POST');' class='menu_lista' href='#'>
   <span>Recepción de Documentos</span>
</a>

该锚点和类“菜单_lista”没有ID或名称,用于同一网页中的其他锚。

我尝试过:

var recepcionLink = myWebDriver.FindElement(By.XPath("//a[@class='menu_lista']/span[text()='Recepción de Documentos']"));
recepcionLink.Click();

但是,我有例外:

OpenQA.Selenium.ElementNotInteractableException:'element不相互作用(会话信息:chrome = 103.0.5060.53)'

看起来我正在获取跨度元素,而不是锚定元素。

On a web page there is this anchor that I want to click using Selenium:

<a onclick='ejecutaOpcion(this, '/srDocumentoAdmRecepcion.do?accion=goInicioGet&estadoDoc=01&coDep=10016','POST');' class='menu_lista' href='#'>
   <span>Recepción de Documentos</span>
</a>

There is no Id or Name for that anchor and class "menu_lista" is used for other anchors in same web page.

I've tried:

var recepcionLink = myWebDriver.FindElement(By.XPath("//a[@class='menu_lista']/span[text()='Recepción de Documentos']"));
recepcionLink.Click();

However, I got exception:

OpenQA.Selenium.ElementNotInteractableException: 'element not interactable (Session info: chrome=103.0.5060.53)'

It looks like I'm getting the span element but not the anchor element.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2025-02-18 08:21:52

您可以尝试使用linkText。

driver.findelement(By.xpath("//span[text()='Recepción de Documentos']/../a).Click();

You can try using linktext.

driver.findelement(By.xpath("//span[text()='Recepción de Documentos']/../a).Click();
我恋#小黄人 2025-02-18 08:21:52

尝试使用相对定位器(Selenium 4中的新定位器)

WebDriver.FindElement(RelativeBy
  .WithLocator(By.CssSelector("a.menu_lista span"))
  .Near(By.TagName("a")));

文档
https:// https://www.selenium.selenium.dev/dev/documentation/webdriveration/webdriveration/webdriveration/webdriveration> /elements/loadators/#可用的销售量

Try using relative locators (new in Selenium 4)

WebDriver.FindElement(RelativeBy
  .WithLocator(By.CssSelector("a.menu_lista span"))
  .Near(By.TagName("a")));

Docs
https://www.selenium.dev/documentation/webdriver/elements/locators/#available-relative-locators

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文