Selenium无法通过XPath定位按钮

Selenium无法通过XPath定位按钮

本文介绍了C#Selenium无法通过XPath定位按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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]"));

该元素的HTML代码在下面

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?

谢谢

推荐答案

如果要查找的按钮的文本为自定义,因为该元素已启用 JavaScript 您必须按如下所示诱使 WebDriverWait :

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

这篇关于C#Selenium无法通过XPath定位按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:52