简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM