当前位置: 首页 > 知识库问答 >
问题:

无法对selenium中禁用的元素使用发送键

吕承福
2023-03-14

我试图用selenium编写代码的输入字段:

input class="tt-hint" type="text" disabled="" spellcheck="off" autocomplete="off" style="position: absolute; top: 0px; left: 0px; border-color: transparent; box-shadow: none; background: none repeat scroll 0% 0% rgb(255, 255, 255);"

我的代码是:

WebElementy inp= driver.findElement(By.className("tt-hint"));

inp.sendKeys(new String[] { "mo" });

但是上面的代码不起作用。我一直得到的错误是:

线程“main”组织中出现异常。openqa。硒。InvalidElementStateException:元素已禁用,因此不能用于操作

非常感谢您的帮助。

我已将代码修改为

js=(JavascriptExecutorjs)驱动程序;js.execute脚本("参数[0]. learveAtcm('禁用')", inp);

共有3个答案

丁善
2023-03-14

下面是Selenium C#的代码,用于隐藏的Textbox/TextArea

IWebElement element;
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("arguments[0].value=arguments[1]", element, inputValueYouWantToPlace);

某些控件无法识别给定的文本,并出现错误,如“请提供输入值”-在这种情况下,请在使用SendKeys()发送值之前启用元素`

js.ExecuteScript("arguments[0].removeAttribute('disabled')", input);
js.ExecuteScript("arguments[0].click()", element);
element.SendKeys(inputValueYouWantToPlace);

`

纪枫
2023-03-14

正如@Saifur所说,Javascript是唯一的选项。但是,这样做也可以删除禁用的属性,或者使用javascript来设置值本身

 WebElement inp = driver.findElement(By.className("tt-hint"));
    //Option 1 remove the disabled attribute
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].removeAttribute('disabled')",inp);
    inp.sendKeys("val");

    //Option 2 go for javascript set value
     js.executeScript("arguments[0].value=arguments[1]",inp,"val");
钦德佑
2023-03-14

例外说明了一切。元素未准备好接受任何交互,已禁用。JavaScript是这里的唯一选项。我会删除disabled属性,然后使用sendKeys()

String script = "document.getElementsByClassName('tt-hint')[1].removeAttribute('disabled')";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(script);

WebElementy inp= driver.findElement(By.className("tt-hint"));
inp.sendKeys("Whatever");
 类似资料:
  • 我有一个粘性导航栏,它有几个列表元素,每个元素都包含href元素。当我试图定位元素时,我得到了错误。以下是我的HTML代码: 我想通过Webdriver定位Academic,我遇到这样的错误无法定位元素。

  • 我试图在元素上执行拖放操作,但它没有发生。 这是我正在处理的页面的片段。在这里,我试图将磁贴“时间”拖动并放置在磁贴“批准”的位置。截图 这是我正在使用的代码。 代码 超文本标记语言 源元素 目标元素 如果你需要更多细节,请告诉我。

  • 我对selenium WebDriver非常陌生,我试图自动化一个页面,它有一个名为“删除日志文件”的按钮。使用FireBug我了解到,HTML被描述为 css选择器也使用firepath定义为“#DeletelogButton” 请帮助我解决这个问题。

  • 下面是selenium找不到元素的代码。 但不知何故,它失败了,对我不起作用。它总是给我以下例外情况:过时的元素引用:元素未附加到页面文档 添加了--GetElementStextList的代码 感谢任何帮助。谢谢

  • 我试图点击一个按钮,它出现在图的右侧(图上方的三行),最后想点击“下载为CSV”。我的代码如下。 File=WebDriverWait(驱动程序,20).until(ec.element_to_be_clickable((by.xpath,'//*[@id=“HighCharts-3SLSA4F-0”]/div[2]/ul/li[6]')).click()#下载为csv 错误:引发TimeoutE

  • 只需要给元素加上 "disabled" class 或者 "disabled" 属性就可以了。 <div class="list-block"> <ul> <li class="item-content"> <div class="item-inner"> <div class="item-title">Element 1</div> </div>