Get value with Selenium C#

天涯浪子 提交于 2020-01-17 06:22:12

问题


I have

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

I need get result "fba4" from data-action-value.

I tried:

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

I think that I need be based on Top highlight to get value fba4 but I don't know how?


回答1:


Use .GetAttribute("data-action-value")

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

You can also use below Xpath

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

You can use

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

Hope it will help you :)




回答2:


To get 'fba4' from the above HTML, try-

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


来源:https://stackoverflow.com/questions/35937489/get-value-with-selenium-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!