元素不可见:元素当前不可见,不能被操纵 - 硒的webdriver

问题描述:

以下是HTML元素不可见:元素当前不可见,不能被操纵 - 硒的webdriver

<div id="form1:customertype" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-state-hover" style="width: 165px;"> 
    <div class="ui-helper-hidden-accessible"> 
     <select id="form1:customertype_input" name="form1:customertype_input" tabindex="-1"> 
     <option value="S">Staff</option> 
     <option value="C">Customer</option> 
     <option value="N">New To Bank</option></select></div> 
    <div class="ui-helper-hidden-accessible"><input id="form1:customertype_focus" name="form1:customertype_focus" type="text" readonly="readonly"></div> 
    <label id="form1:customertype_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all" style="width: 149px;">Staff</label> 
    <div class="ui-selectonemenu-trigger ui-state-default ui-corner-right ui-state-hover"><span class="ui-icon ui-icon-triangle-1-s ui-c"></span></div></div> 

类的样式表=“UI辅助隐藏可访问”是

ui-helper-hidden-accessible { 
     border: 0; 
     clip: rect(0 0 0 0); 
     height: 0px; 
     margin: -1px; 
     overflow: hidden; 
     padding: 0; 
     position: absolute; 
     width: 0px; 
    } 

以下是我的代码

WebElement customerType = driver.findElement(By.id("form1:customertype_input")); 
    Select select = new Select(customerType); 
    select.selectByVisibleText("New To Bank"); 

当我尝试从下拉菜单中选择“新建行”我得到的例外 元素不可见:元素当前不可见,可能无法操作 - 硒webdriver

我试过WebDriverWait技术,但没有用,任何想法?

+0

是否打开下拉菜单? – 2014-09-05 06:27:35

+0

可能是选择下拉列表包装。这通常发生在创建爵士乐下拉菜单时。检查它是否真的隐藏。 – Vinay 2014-09-05 06:33:44

+0

是的手动下拉打开,但不是从代码。该下拉菜单在页面上直观显示。 – 2014-09-05 06:42:52

尝试customerType进行点击您的创建对象之前选择

+0

同样的结果:(我得到这一行的异常 select.selectByVisibleText(“New To银行“); – 2014-09-08 10:38:39

+0

使用其他标识符点击内容的索引,将所有选项标签放入列表中,并按索引编号2选择_新到银行选项 – 2014-09-08 13:30:05

嗯,我发现周围的工作,以解决我的问题,但我不开心的与此有关。无论如何,我所做的是集中在通过点击控制下拉菜单的div元素,然后发送箭头键两次,然后按Enter键。这选择我想要的选项。我用了下面的方法

driver.switchTo().activeElement() 

我也有同样的问题,几个小时后,我意识到浏览器试图点击页面加载之前的元素。

所以我创建了一个睡眠解决了这个问题:

sleep(1) 

附: - 这是我真的不喜欢的解决方案。 我只是指出你的原因。 您可以做的最好的方法是检查您遇到问题的页面,并尝试优化加载时间。

我不相信该选项的文本实际上是可见的,然后再尝试选择它。尝试按值选择。

WebElement customerType = driver.findElement(By.id("form1:customertype_input")); 
Select select = new Select(customerType); 
select.selectByValue("N"); 

虽然您可能需要实际点击选择器才能选择选项。

WebElement customerType = driver.findElement(By.id("form1:customertype_input")); 
new WebDriverWait(driver, 15).until(
      ExpectedConditions.elementToBeClickable(customerType)); 
customerType.click(); 

Select select = new Select(customerType); 
select.selectByValue("N");