Search code examples
c#seleniumselenium-webdriverxpathhtmlelements

C# selenium unable to locate button through xpath


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


Solution

  • 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']")));