2016-09-26 34 views
0

我有按钮,在网页上,看起来像这样:硒返回IWebElement通过按钮上的文字

<button type="button" class="btn btn-primary btn xs-width-100" onclick="startScreening(0, 'A', 'UK');">Start Screening</button>` 

此按钮有一个非常相似的结构,页面上的其他按钮。我正在尝试使用Selenium WebDriver来查找此元素。我目前正在使用此:

return UFEElement<IWebElement>(FieldType.Link, By.XPath("//button[@class=\"btn btn-primary btn xs-width-100\"]")); 

我不能class找到,因为页面上的其他按钮具有相同的class,我不能onclick找到,因为根据该值的变化是以前的操作。

我试过By.XPath("//*[contains(text(), 'My Button')]")和类似的方法,但它不起作用。

我得到一个错误: -

Error: An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code.

有另一种方式来找到这个元素?

+0

这个定位器有什么问题'By.XPath(“// * [contains(text(),'Start Screening')]”)'??有什么异常吗? –

+0

错误:'WebDriver.dll中发生类型'OpenQA.Selenium.NoSuchElementException'的异常,但未在用户代码中处理。 – Ross

+0

你找帧/ iframe了吗?确保这个元素不在任何框架/ iframe中? –

回答

0

为什么不使用css?

button[onclick*='startScreening']
button[onclick*='startScreening'][onclick*='UK']

0

An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code

它可以定时的问题,你应该尝试使用WebDriverWait等到元素可见如下的可点击: -

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3)) 
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpth(".//button[text() = 'Start Screening']"))); 

或者你也可以找到它也与onclick功能的部分匹配为: -

IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button[onclick*='startScreening']")));