使用 Selenium C# 获取价值

Get value with Selenium C#

我有

`<button class="button button--chromeless" data-action="select-anchor" data-action-value="fba4">Top highlight</button>`

我需要从数据操作值中获取结果 "fba4"。

我试过了:

IWebElement ell = driver.FindElement(By.CssSelector("button[data-action-value]"));

我认为我需要基于 Top highlight 才能获得值 fba4 但我不知道如何?

使用.GetAttribute("data-action-value")

 string element  = driver.FindElement(By.CssSelector("button[data-action-value]")).GetAttribute("data-action-value");

你也可以使用下面的Xpath

//button[@class='button button--chromeless']/@data-action-value

您可以使用

string element  = driver.FindElement(By.ClassName("button button--chromeless")).GetAttribute("data-action-value");

希望对您有所帮助:)

要从上面的 HTML 得到 'fba4',请尝试-

string ell = driver.FindElement(By.CssSelector("button[class='button button--chromeless']")).GetAttribute("data-action-value");