2016-09-26 39 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']")));