根据Selenium Webdriver所包含的文本选择一个元素 [英] Select an element by the text it contains with Selenium Webdriver

查看:1050
本文介绍了根据Selenium Webdriver所包含的文本选择一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jjust开始与Selenium webdriver,我有一个问题,直接涉及的按钮,我想选择/点击所有没有ID,并共享同一个类。

I've jjust started off with Selenium Webdriver and I've hit an issuee straight away involving the buttons I'm trying to select/click all have no IDs and share the same class.

因此,我想知道如何通过它们包含的唯一文本来选择这些。

So I'm wondering how I select these by the unique text they contain.

可能与css选择器可能,但我不确定如何也告诉它寻找特定的文本来选择元素。

I'm thinking possibly with css selector maybe, but I'm unsure how to also tell it to look for specific text to select the element.

我目前拥有的是:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Internal;

namespace SeleniumTest1.Methods
{
    public class HomePage
    {

        private readonly IWebDriver _browser;
        public HomePage(IWebDriver browser)
        {
            _browser = browser;
        }

        public IWebElement SearchBox()
        {
            return _browser.FindElement(By.Id("searchBox"));
        }

        public void ImageButton()
        {
            _browser.FindElement(By.CssSelector("a")).Click();
        }

    }
}

到目前为止。

我在哪里有CssSelector我不知道该怎么说选择a包含文本xyz。

Where I have the CssSelector I'm not sure if theres anyway to say select "a" containing text "xyz".

我已经尝试搜索的方式,但找不到任何东西,虽然我觉得这必须是一个已经提出的问题,谢谢。

I've tried search for ways but can't find anything, though I feel this must be an issue which has been raised before, thanks.

推荐答案

如果使用 xpath ,相当容易。
如果它有唯一的文本您的 xpath 应该看起来像这样

Fairly easy if you use xpath. if it has unique text your xpath should look like something like this

//button[.='xyz']

指向HTML层次结构中的父级,并只查找文本

So, here "." points to the parent in HTML hierarchy and just look for text

这篇关于根据Selenium Webdriver所包含的文本选择一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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