【发布时间】:2017-07-13 23:13:22
【问题描述】:
提交的代码仅用于证明 PhantomJS 与 Selenium 的使用概念。
我正在测试一个单页应用程序。
代码在 GoogleChrome 和 FireFox 上运行良好,但在 PhantomJS 中找不到元素。
问题是 驱动程序在登录页面上找到“登录”按钮(通过 xpath)但是当用户在登录表单上重定向时尝试通过xpath 定位元素后抛出TimeoutException:ExpectedCondition failed。
我正在使用:
- 硒 3.4.0
- PhantomJS 2.1.1
我的代码:
public class GoogleSearchTest
{
private WebDriver driver;
private WebDriverWait wait;
@Before
public void SetUp()
{
System.setProperty("phantomjs.binary.path", "./src/test/resources/drivers/phantomjs.exe");
driver = new PhantomJSDriver();
driver.manage().window().maximize();
driver.get("https://dev.investmentpunk.academy");
wait = new WebDriverWait(driver, 10);
}
@Test
public void testGoogleSearch() throws InterruptedException
{
WebElement Login_btn = driver.findElement(By.xpath("//ul/li[@class='ng-scope'][1]"));
Login_btn.click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='c-input-db ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required ng-valid-minlength ng-valid-maxlength']"))).sendKeys("user1");
WebElement Password = driver.findElement(By.xpath("//div[@class='form-row'][2]/input[@class='c-input-db ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required']"));
Password.sendKeys("qwerty");
WebElement Login = driver.findElement(By.xpath("//button[@class='btn btn-blue']"));
Login.click();
}
@After
public void tearDown()
{
driver.quit();
}
请帮我解决这个问题。
======================编辑部分======================
所以,情况是我正在使用您的 xpathes 使用 TestNG(或使用 JUnit)运行代码,并且它一直有效,直到昨天。因此,驱动程序无法找到 WebElement Login_btn = driver.findElement(By.xpath("//ul/li[@class='ng-scope']/a[text()='EINLOGGEN']")); 我没有在代码中提供任何重大更改:
public class GoogleSearchTest
{
private WebDriver driver;
private WebDriverWait wait;
//second commit mess
@BeforeTest
public void SetUp()
{
System.setProperty("phantomjs.binary.path", "E:/PhantomJS/phantomjs-2.1.1-windows/bin/phantomjs.exe");
driver = new PhantomJSDriver();
System.out.println("=====PhantomJS Driver Initiated=====");
driver.manage().window().maximize();
driver.get("https://dev.investmentpunk.academy/");
System.out.println("=====URL Accessed=====");
wait = new WebDriverWait(driver, 10);
}
@Test
public void testGoogleSearch() throws InterruptedException
{
WebElement Login_btn = driver.findElement(By.xpath("//ul/li[@class='ng-scope']/a[text()='EINLOGGEN']"));
Login_btn.click();
System.out.println("=====EINLOGGEN button Clicked=====");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@placeholder='Username/Email']"))).sendKeys("user1");
System.out.println("=====Username Sent=====");
WebElement Password = driver.findElement(By.xpath("//input[@placeholder='Passwort']"));
Password.sendKeys("qwerty");
System.out.println("=====Password Sent=====");
WebElement Login = driver.findElement(By.xpath("//button[@class='btn btn-blue']"));
Login.click();
System.out.println("=====Login button Clicked=====");
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}
【问题讨论】:
标签: java selenium phantomjs single-page-application