Our Devs are using data-auto-id within the latest project

  1. makes my scripting hard and
  2. selenium does not recognise it as an ID unfortunately.

Can anyone tell me how in my POM i can identify data-auto-id within C#/.NET without a huge code overhead.

Currently I am writing my POM as per below, so if we can stick to this format that would be great.

public IWebElement PWReset { get { return _driver.FindElement(By.CssSelector("CSS Data here, too long to post")); } } 

HTML for this example

<div>Forgotten your password?</div> 

Code to Guys Suggestion

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); public IWebElement PWReset { get { return _driver.FindElement(By.CssSelector("[data-auto-id='Password_Reset']")); } } 

Error being produced is as follows: OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"[data-auto-id='Password_Reset']"} (Session info: chrome=77.0.3865.90)'

10

4 Answers

The below expression is not ID its a cssSelector syntax

#Password_Reset > div > div 

Either use By.CssSelector instead of By.Id

or if Password_Reset is your id you can use xpath as:

By.Xpath("//*[@id='Password_Reset']/div/div") 

Add HTML code for exact locator

2

Try using the below xPath

//div[@data-auto-id='Password_Reset'] 

You can use CssSelector to locate by attribute

_driver.FindElement(By.CssSelector("[data-auto-id='Password_Reset']")); 

You might need to use wait:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("[data-auto-id='Password_Reset']"))); 
4

The answer turns out to be alot more simpple than we think, using the following does/will work.

public IWebElement SubmitDetails { get { return _driver.FindElement(By.CssSelector("[data-auto-id='login_button_submit'")); } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy