3

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

3

You could use a CSS selector, such as:

By.CssSelector("[data-id='6948']")
answered Feb 26, 2016 at 20:14

Comments

2

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

1

You can use xpath:

driver.FindElement(By.xpath('//a[@data-id="6948"]'));
answered Feb 26, 2016 at 20:28

1 Comment

This method is a last resort. When selecting using xpath we make the code brittle every small change will break our tests

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.