【问题标题】:Selenium : org.openqa.selenium.ElementNotInteractableException:硒:org.openqa.selenium.ElementNotInteractableException:
【发布时间】:2017-12-30 22:10:00
【问题描述】:

这就是 HTML 的样子。

<select name="ctl00$bodyContent$ddl_Territory" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$bodyContent$ddl_Territory\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl00_bodyContent_ddl_Territory" class="select2-container" style="margin-left: 3%;">
    <option selected="selected" value=""></option>
    <option value="675">ALASKA AK (OCRGA061A) - HCP</option>
    <option value="271">ALBANY GA (OCBDB041A) - HCP</option>
    <option value="125">ALBANY NY (OCBAA031A) - HCP</option>
    <option value="126">ALBANY NY (OCBAA032A) - HCP</option>
    <option value="426">ALBANY NY (OCRAA031A) - HCP</option>
    ....
    <option value="427">ALBANY NY (OCRAA031A) - HOSPITAL</option>

不断失败的自动化代码 -

Select territory = new Select(driver.findElement(By.name("ctl00$bodyContent$ddl_Territory")));
        System.out.println("selected");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block 
           // Trying to figure if sleep is required or not.. 
            e.printStackTrace();
        }



        List<WebElement> optionList = territory.getOptions();
        for (WebElement webElement : optionList) {
            System.out.println(webElement.getText()); //this print nothing..empty line..confused
        }

        territory.selectByIndex(1); //exception thrown here

当我打电话给selectByIndex 时,我得到org.openqa.selenium.ElementNotInteractableException:。我已经尝试过 selectByValue 等。但也没有帮助。 我确信 territory 指向正确的元素,因为 optionList 计数与 HTML 中的选项数相同。但是不确定为什么文本没有在循环中打印以及为什么异常..

驱动程序 - geckodriver-v0.18.0-win64

TIA!

更新 1:webElement.isDisplayed() 打印 false 以获取所有选项。 getLocation() 打印 0,0 用于 X 和 Y 坐标..

【问题讨论】:

  • 你想在这里做什么List&lt;WebElement&gt; optionList = territory.getOptions();?谢谢
  • 什么想睡 10 秒? try { Thread.sleep(10000); }
  • List&lt;WebElement&gt; optionList = territory.getOptions(); 我试图在 Select 中获取选项列表并将它们保存在内存中以备后用。睡眠是我正在尝试的东西。
  • 你能分享一个你正在使用的链接吗[如果可能的话]?这将有助于理解您的问题。我可以尝试在此编写脚本。
  • 你试过其他浏览器吗?

标签: java selenium selenium-webdriver geckodriver


【解决方案1】:

获取所有选项的替代方法如下

List<WebElement> optins = driver.findElements(By.xpath("//select[@name='ctl00$bodyContent$ddl_Territory']/options"));

通过可见文本选择的替代方法

driver.findElement(By.name("ctl00$bodyContent$ddl_Territory")).sendKeys("ALBANY NY (OCBAA031A) - HCP");

【讨论】:

  • 感谢@Gaurang!另一种方法确实有效..关于为什么我的方法不起作用的任何想法?
  • 你的方法是一种标准方法,应该可以工作,一定是 webdriver 或 geckdriver 或 firefox 版本有问题。您可以尝试更改它们。但是,如果这解决了您的问题,那就去吧。 stackoverflow.com/help/someone-answers
猜你喜欢
  • 2018-04-02
  • 2018-10-08
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 2020-11-16
  • 2021-03-10
  • 1970-01-01
相关资源
最近更新 更多