C# selenium unable to locate button through xpath

半世苍凉 提交于 2021-02-16 21:18:06

问题


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


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


来源:https://stackoverflow.com/questions/49576187/c-sharp-selenium-unable-to-locate-button-through-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!