Nico
Nico

Reputation: 374

Why Can't found this element with WebDriver?

I am starting in the world of UI automation with WebDriver and Java. I am getting a problem when I try to select a element of a combo box. This is the code:

WebDriver driver = new FirefoxDriver();
driver.get("http://intersite.com/");
new Select(driver.findElement(By.xpath(".//*[@id='term']"))); //Exception happens in this line org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='term']"}

And this is the code in the web site (I use Firepath to know the Xpath):

<select name="term" onchange="getTipDoc('');" id="term" class="termination"><option value="">-- Select an Option --</option>
                            <option value="OPT1">Option 1</option>
<option value="OPT2">Option2</option>
</select>

I see in the tag select, the ID attribute is correct but the exception always happens. I tried with othe method for locate the element like "By.id" but doesn't work too. What can i do?

Regards!

Upvotes: 0

Views: 166

Answers (2)

Alaa Abuzaghleh
Alaa Abuzaghleh

Reputation: 1009

you need to wait to get the page load before trying to get its element this code will help you to do that

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='term']")));

Upvotes: 1

Saifur
Saifur

Reputation: 16201

Couple of possible reasons can happen in case like this

  • Element you are looking for is inside an iframe. Use driver.switchTo().frame(driver.findElement(somethting));
  • Element look up is faster than the load time. In this case use explicit wait. WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("term"))); See this
  • There are duplicate Ids. try using select[id='term'][class='termination'] as cssSelector

And, of course use By.id() since the id is available.

Upvotes: 2

Related Questions