我是从WebDriver和Java的UI自动化世界开始的。当我试图选择组合框的一个元素时,我遇到了一个问题。这是代码:
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']"}
这是网站中的代码(我使用Firepath来了解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>
我在标签select中看到,ID属性是正确的,但是异常总是会发生。我尝试使用too方法来定位像"By.id“这样的元素,但是也不起作用。我能做什么?
致以问候!
发布于 2015-05-18 21:39:53
在这种情况下,可能会有几个可能的原因。
iframe
中查找的。使用driver.switchTo().frame(driver.findElement(somethting));
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("term")));
见这select[id='term'][class='termination']
作为cssSelector当然,还可以使用By.id()
,因为id是可用的。
发布于 2015-05-18 21:48:56
您需要等待获得页面加载,然后尝试获取其元素--这段代码将帮助您做到这一点。
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='term']")));
https://stackoverflow.com/questions/30313215
复制相似问题