我在使用 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。