2017-03-16 50 views
0

我想在无头浏览器中进行一些测试,以测试填充表单。PhantomJS和Selenium - 在搜索元素时尝试使用等待时遇到问题

我使用,我是从下面的示例所示的代码:

github link to example of using phantomjs

我的代码:

WebDriverWait wait = new WebDriverWait(driver, 30); 
driver.get("<URL HERE, left out for privacy on stack overflow!>"); 
By amount = By.id("amountField"); 
wait.until(ExpectedConditions.presenceOfElementLocated(amount)); 

错误的IntelliJ:

until(java.util.function<? super org.openqa.selenium.WebDriver, V) in FluentWait cannot be applied to : 

(org.openqa.selenium.support.ui.ExpectedCondition<org.openqa.selenium.WebElement>) 

我不是我真的很确定我在哪里错了 - 但我花了两个小时尝试解决这个问题。

元素我要找页:

<input name="amount0" id="amountField" value="" class="amount form-control" type="number" pattern="\d+(\.\d*)?" maxlength="10" placeholder="Amount"> 
+0

你能对你的问题有点特别吗? 1.你的目标是什么? 2.你想达到什么目的? 3.为什么你需要一个明确的等待? 4.它是一个隐藏的元素? 5.它是如何出现的? 6.提供HTML DOM。 – DebanjanB

+0

请将上面的内容作为流利等待而不是显式等待。 – DebanjanB

+0

试图测试填写表单,我需要流利的等待,因为phantomjs似乎试图在加载之前获取元素。元素不隐藏。它显示为一个测试框。不幸的是,我没有完整的DOM页面,但是现在将该元素粘贴到主要问题中。 – MickeyThreeSheds

回答

1

看来,这个问题是不是与PhantomJS。你看到的错误告诉了这一切。

你的代码似乎考虑WebDriverWait wait作为流利的等待在那里,你已经出口import org.openqa.selenium.support.ui.ExpectedConditions;这是造成错误。

在文档以及,笔者已经使用显式等待进口import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;

我建议你试试这个方法:

  1. 让网址,以获取第一次加载。
  2. 定义显式等待。
  3. 直到定义ExpectedConditions。
  4. 获取元素的状态。
  5. Sysout状态。
  6. 如果失败,请增加显式等待的时间以完成ExpectedConditions。

你可以看看这个修改后的代码:

driver.get("<URL HERE, left out for privacy on stack overflow!>"); 
WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("amountField"))); 
     boolean status=element.isDisplayed(); 

     if (status) 
     { 
      System.out.println("Element is displayed"); 
     } 
     else 
     { 
      System.out.println("Element is not displayed"); 
     } 

让我知道如果这能帮助你。

+0

你有机会看看这个解决方案吗? – DebanjanB

+0

@MickeyThreeSheds如果本答案满足您的问题,请您接受答案以结束讨论? – DebanjanB

+0

对不起 - 花了我一段时间回到你身边 - 我的兄弟正在死去,这有时会让我离开我的日常活动。 – MickeyThreeSheds

相关问题