I have this:
<a data-id="6948" class="klasax" href="show.aspx?xid=....;" title="Test">Test</a>
I trying to click based on data-id="6948"
My code:
IWebElement cl = driver.FindElement(By.Id("6948");
cl.Click();
Clearly problem is on data-id
How I can click on it based on data-id ?
asked Feb 26, 2016 at 20:12
3 Answers 3
You could use a CSS selector, such as:
By.CssSelector("[data-id='6948']")
answered Feb 26, 2016 at 20:14
Comments
data-id
is not id
, you can't find it using By.Id
selector. You can use CssSelector
driver.FindElement(By.CssSelector("[data-id='6948']"));
Or ClassName
driver.FindElement(By.ClassName("klasax"));
answered Feb 26, 2016 at 20:17
Comments
You can use xpath
:
driver.FindElement(By.xpath('//a[@data-id="6948"]'));
answered Feb 26, 2016 at 20:28
1 Comment
Mickey Perlstein
This method is a last resort. When selecting using xpath we make the code brittle every small change will break our tests
lang-cs