使用webdriver找不到元素

时间:2013-02-04 11:32:55

标签: java selenium webdriver selenium-webdriver xpath

有一个网络元素:

<select id="sel" onchange="refresh('sel')">
  <option value="1"> A</option>
  <option value="2"> B</option>
</select>

因为它有onchange,我无法选择该选项。我的代码是这样的:

new Select(driver.findElement(By.id("sel"))).selectByValue("1");

但它不起作用。有可能让它发挥作用吗?

2 个答案:

答案 0 :(得分:1)

使用xpath而不是使用id。因为根据代码,一旦你选择了值,它就会刷新屏幕。尝试使用xpath并在xpath之后给出一些等待时间进行刷新。

答案 1 :(得分:0)

我将尝试这样做:(C#)

IWebElement element = driver.FindElement(By.XPath(XPATH_FOR_OPTION));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].fireEvent('onclick');", element);