在硒中按文本选择元素 [英] Select Element By Text in Selenium

查看:51
本文介绍了在硒中按文本选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很简单,但是为什么没有方法可以通过它来查找元素内部文本而不使用xpath?例如,有一个元素:

This may sound so simple but why there is no method to find element by its inner text without using xpath? for instance there is an element:

<button>Some Text</button>

,并且Selenium没有提供任何通过内部文本选择它的方法.像这样:

and Selenium does not provide any methods to select it by inner text. Something like:

driver.findElement(By.innerText("Some Text");

通过内部文本查找元素的另一种方法是什么?我非常了解xpath,但是由于项目的限制,我无法使用它.

What is the the other way to find element by inner text? I am well aware of xpath, but I can not use it because of restrictions of the project.

推荐答案

有一个

There is a linkText method:

driver.findElement(By.linkText("Some Text"))

您还具有 partialLinkText :

driver.findElement(By.partialLinkText("Partial Text"))

有关示例,请参见正如@Cajova_Houba提到的那样,它仅适用于锚元素< a>

As @Cajova_Houba mentioned that it works only for anchor elements <a>

这篇关于在硒中按文本选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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