Reputation: 374
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
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
Reputation: 16201
Couple of possible reasons can happen in case like this
iframe
. Use driver.switchTo().frame(driver.findElement(somethting));
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("term")));
See this select[id='term'][class='termination']
as cssSelectorAnd, of course use By.id()
since the id is available.
Upvotes: 2