Tingting
Tingting

Reputation: 157

Can not find element using webdriver

There is a web element:

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

Because it has onchange, I can not select the option. My code is like this:

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

But it can not work. Is it possible to make it to work?

Upvotes: 1

Views: 646

Answers (2)

GirishB
GirishB

Reputation: 544

This is what I'll try to do:(C#)

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

Upvotes: 0

Umamaheshwar Thota
Umamaheshwar Thota

Reputation: 521

Use the xpath instead of using id. Because as per the code, it will refresh the screen once u select the value. Try using xpath and after the xpath give some wait period for refreshing.

Upvotes: 1

Related Questions