Our Devs are using data-auto-id within the latest project
- makes my scripting hard and
- 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)'
104 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
2Try 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']"))); 4The 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'")); } }