Java 能够使用selenium定位框架,但无法使用Junit Cumber
我能够使用selenium java程序定位帧。以下代码成功运行:Java 能够使用selenium定位框架,但无法使用Junit Cumber,java,selenium,junit,cucumber,cucumber-junit,Java,Selenium,Junit,Cucumber,Cucumber Junit,我能够使用selenium java程序定位帧。以下代码成功运行: public static void TestCase() throws IOException, InterruptedException{ WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("w
public static void TestCase() throws IOException, InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("website");
driver.findElement(By.xpath("//*[@id='username']")).sendKeys(userName);
driver.findElement(By.xpath("//*[@id='password']")).sendKeys("password");
driver.findElement(By.xpath("//*[@id='session_link']")).click();
driver.findElement(By.xpath("//*[@id='app_236']/a")).click(); //Click a link on the page
driver.switchTo().frame("iframe100-100"); //Switch to a frame within the new page
driver.findElement(By.xpath("//*[@id='yui-gen0']/li[2]/div")).click(); //Click element within the frame
}
但是如果我使用Cucumber Junit maven项目运行相同的代码,程序将无法定位帧
@Given("^I log into website$")
public void log_in()
{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("website");
driver.findElement(By.xpath("//*[@id='username']")).sendKeys(userName);
driver.findElement(By.xpath("//*[@id='password']")).sendKeys("password");
driver.findElement(By.xpath("//*[@id='session_link']")).click();
}
@When("^I select the link from homepage$")
public void select_link() throws InterruptedException
{
driver.findElement(By.xpath("//*[@id='app_236']/a")).click();
}
@Then("^I should see something$")
public void click_element_within_frame()
{
driver.switchTo().frame("iframe100-100"); //Switch to a frame within the new page
driver.findElement(By.xpath("//*[@id='yui-gen0']/li[2]/div")).click(); //Click element within the frame
}
错误消息:
31morg.openqa.selenium.NoSuchFrameException:无法定位帧:iframe100-100
有什么建议吗?iframe100-100中的数字可能是动态的。app_236和yui-gen0可能也是这样。那些XPath看起来很恶心!为什么不使用更易读的东西,比如By.idusername?谢谢@SiKing。原来相同的iframe为不同的用户提供了不同的数字,这些用户按照您所说的登录-动态。我无法识别iframe的唯一特性。所以我试着通过索引找到它。我从0-3开始尝试,直到单击其中的元素。结果是,指数是2。