我正在尝试编写脚本来自动登录和注销站点www.flipkart.com。但是我的脚本失败并给出异常:
Unable to locate element: {"method":"link text","selector":"Logout"}
我无法找出问题所在。谁能告诉我定位器有什么问题。
下面是我的代码:
Actions builder = new Actions(driver);
System.out.print("log1");
WebElement element = driver.findElement(By.xpath(".//*[@id='fk-mainhead-id']/div[1]/div/div[2]/div[1]/ul/li[6]/a"));
System.out.print("log2");
Action action = builder.moveToElement(element).build();
action.perform();
System.out.print("log3");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.findElement(By.linkText("Logout")).click();
}
最佳答案
您很幸运。.最近,我帮助一个人登录和退出了flipkart。这是脚本:
@Config(url="http://flipkart.com", browser=Browser.FIREFOX)
public class TestFlipKart extends Conductor {
@Test
public void testLoginLogout() {
String username = "<username>";
String password = "<password>";
click(By.cssSelector("a[href*='/login']"))
.setText(By.cssSelector("input[name='email']"), username)
.setText(By.cssSelector("input[name='password']"), password)
.click(By.cssSelector("input[type='submit'][value='Login']"))
.validatePresent(By.cssSelector("li.greeting-link > a"))
.hoverOver(By.cssSelector("li.greeting-link > a"))
.click(By.cssSelector("ul.account-dropdown a[href*='/logout']"))
// should be logged out now.
.validatePresent(By.cssSelector("a[href*='/login']"));
}
}
请注意,这是使用Conductor框架的。您可以将我在那里的CSS选择器转换为脚本。