通过显示文本查找含硒元素 [英] Find element with selenium by display text

查看:52
本文介绍了通过显示文本查找含硒元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用硒将鼠标悬停在菜单栏中的某个元素上,但是很难找到该元素.元素显示在下面:

I am trying to hover over an element in a menu bar with selenium, but having difficulty locating the element. The element is displayed below :

<DIV onmouseover="function(blah blah);" class=mainItem>TextToFind</DIV>

此类型有多个元素,因此我需要通过TextToFind查找该元素.

There are multiple elements of this type so I need to find this element by TextToFind.

我尝试过:

driver.FindElement(By.XPath("TextToFind"))

driver.FindElement(By.LinkText("TextToFind")) 

这两个都不起作用.我什至尝试过:

which both didn't work. I even tried:

driver.FindElement(By.ClassName("mainItem")) 

这也不起作用.有人可以告诉我我做错了什么吗?

which also did not work. Can someone tell me what I am doing incorrectly?

推荐答案

您在By.Xpath中使用了错误的xpath语法,并且By.LinkText仅适用于带有文本的a元素,并且By.ClassName看起来不错,但可能有更多具有该类名称的元素,这就是为什么您无法获得正确的元素的原因,因此,您应尝试在提供的xPath中使用以下文本:-

You are using incorrect syntax of xpath in By.Xpath and By.LinkText works only on a element with text and By.ClassName looks ok but may be there are more elements with that class name that's why you couldn't get right element, So you should try use below provided xPath with text :-

driver.FindElement(By.XPath("//div[text() = 'TextToFind']"));

driver.FindElement(By.XPath("//div[. = 'TextToFind']"));

driver.FindElement(By.XPath("//*[contains(., 'TextToFind')]"));

希望它能起作用...:)

Hope it works...:)

这篇关于通过显示文本查找含硒元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆