我对selenium webdriver有困难。 这有效:
@Test
public void testFindElements()throws Exception{
userName = driver.findElement
(By.id("j_userparam"));
userName.sendKeys("abc");
password = driver.findElement(By.id("j_userpwd"));
password.sendKeys("abc");
submit = driver.findElement(By.xpath("//div/button"));
submit.click();
Thread.sleep(5000);
driver.switchTo().defaultContent();
WebElement iframe=driver.findElement(By.xpath(".//*[@id='iframeMain']"));
driver.switchTo().frame(iframe);
WebElement element=driver.findElement(By.xpath(".//*[@id='oim']"));
element.click();
Thread.sleep(5000);
}
但如果我在不同的测试注释中编写下半部分,它就不起作用:
@Test
public void Search() throws Exception{
driver.switchTo().defaultContent(); // you are now outside both frames
WebElement iframe=driver.findElement(By.xpath(".//*[@id='iframeMain']"));
driver.switchTo().frame(iframe);
WebElement element=driver.findElement(By.xpath(".//*[@id='oim']"));
element.click();
Thread.sleep(5000);
}
我收到错误“无法找到xpath。