1

I'm trying to locate an element using XPath but my code is incorrect and I'm not sure of the write syntax.

I entered the code below which isn't working.

IWebElement customizeButton = driver.FindElement(By.XPath("//*[@id='container']/div/div[1]/div[1]/div[2]/button[2]"));

The HTML code for the element is below

<button class="button u-space-ls js-customize-button button--primary " data-tooltip="{&quot;placement&quot;:&quot;left&quot;,&quot;title&quot;:&quot;Customize&quot;}" data-reactid=".0.0.0.3.3"><span class="icon icon-gear" data-reactid=".0.0.0.3.3.0"></span><span class="u-small-hidden u-medium-hidden" data-reactid=".0.0.0.3.3.1"> Customize</span></button>

Can you please let me know how I should correct my code?

Thanks

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Nicole
  • 104
  • 1
  • 2
  • 11
  • and how shall we check this xpath without the html? you only showed us a button tag. – derloopkat Mar 30 '18 at 14:28
  • Can you provide the html ? Or maybe you can add an id to that element and it will be very simple to locate that element. – Vasilut Lucian Mar 30 '18 at 14:34
  • Note that using the suggested XPath is not recommended, as it's very brittle. Many small changes in the page can cause it to stop being correct. The rule of thumb is to rely on fewer details as possible that have high risk of changing (like the number of inner divs and their ordinal indices), as long as they identify the element uniquely. This is why Id is usually the preferred way if it's available and not auto generated. Class and *simple* CssSelectors are also fine as long as they follows that rule. – Arnon Axelrod Mar 31 '18 at 20:22

1 Answers1

1

If you are looking to locate the button with text as Customize as the element is JavaScript enabled element you have to induce WebDriverWait as follows :

wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement customizeButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='button u-space-ls js-customize-button button--primary']//span[@class='u-small-hidden u-medium-hidden']")));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you. I had to use the ClassName but this worked perfectly. IWebElement customizeButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.ClassName("js-customize-button"))); – Nicole Mar 30 '18 at 20:24