元素不可见:元素当前不可见且可能无法操作-Selenium webdriver

元素不可见:元素当前不可见且可能无法操作-Selenium webdriver,selenium,Selenium,下面是html <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=

下面是html

<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>
下面是我的代码

    WebElement customerType = driver.findElement(By.id("form1:customertype_input"));
    Select select = new Select(customerType);
    select.selectByVisibleText("New To Bank");
当我尝试从下拉列表中选择New to Bank时,会出现异常 元素不可见:元素当前不可见且可能无法操作-Selenium webdriver


我尝试过WebDriverWait技术,但没有用,有什么想法吗?

在创建Select对象之前尝试执行click on customerType

好吧,我找到了解决问题的方法,但我对此不满意。无论如何,我所做的是把重点放在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");

我也遇到同样的问题。我试过很多方法。 最后,下面的python代码解决了这个错误。 在选择选项之前,我使用javascript代码使元素可见

css='selectstate'元素的css选择器 js=const data_options=Array.fromdocument.querySelectorAll'{css}'; data_options.forEacha=>{{a.style='display:block;';};。formatcss=css self.driver.execute_scriptjs
也许这对你有帮助

下拉列表是否打开?选择下拉列表可能已包装。这通常发生在创建爵士下拉列表时。检查它是否真的隐藏。是的,手动打开下拉列表,但不是从代码中打开。下拉列表在页面上可见。请尝试通过父div:WebElement customerType=driver.findElementBy.Xpath//div[@id='form1:customerType']//select[@id='form1:customerType\u input']访问它,但结果相同。有趣的是,直到这一行select.selectByVisibleTextNew To Bank,它才抛出异常;相同的结果:我在这一行中得到异常选择。selectByVisibleTextNew To Bank;使用其他标识符单击内容的类似索引,将所有选项标记放入列表中,然后按索引编号2选择以选择“新建银行”选项
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");