如何忽略要点击的元素,尽管不可见Selenium

时间:2015-07-15 03:31:02

标签: java jquery selenium

我正在研究selenium,在运行Java代码时,我尝试从网页访问文本框但是selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时错误进行交互。

文字字段的HTML代码:

<input type="text" name="TotalTaxPercent" id="TotalTaxPercent" value="19.00" class="smallinputField rightAlign" size="7" onblur="javascript:validateDecimal(this, 5)">

访问文本字段的JAVA代码:

public void setItemTaxValue( String value){
  //By writableTag = By.name("TotalTaxPercent");
  By writableTag = By.xpath("//a[contains(@title,'Override total tax percent')]"); 
  this.sleep(3);
  if (this.waitForExistence(writableTag,35))    {
    this.textfieldSetText(writableTag, value);
    clickOnOK ();
    //          
  } else{
    JLog.fail("Unable to find a writable item taxdialog!");
  }         
}
  

错误树:

[2015-07-14 20:18:34 PDT] Switch to Frame: <top>
[2015-07-14 20:19:22 PDT] Setting TextField (By.name: TotalTaxPercent) with data: 10
[2015-07-14 20:19:24 PDT] Screen Capture: C:\source\selenium-main\selenium-vodafone\target\capture\screenCapture_20150714201922308.jpg
[2015-07-14 20:19:24 PDT] FAIL: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 40 milliseconds

请告知,谢谢你

1 个答案:

答案 0 :(得分:2)

可能使用JavaScript执行整个操作,因为它是一个隐藏的字段

String script = "document.getElementById('TotalTaxPercent').setAttribute('value','20.00');";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(script);

如果JQuery是一个选项,那么尝试

String script = "$('#TotalTaxPercent').prop('value', 20.00);";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(script);