Selenium textarea-在Javascript中-解释
我有一段html,它位于下拉列表下,在下拉列表元素中选择somthing后显示:Selenium textarea-在Javascript中-解释,javascript,selenium,selenium-webdriver,selenium-chromedriver,selenium-ide,Javascript,Selenium,Selenium Webdriver,Selenium Chromedriver,Selenium Ide,我有一段html,它位于下拉列表下,在下拉列表元素中选择somthing后显示: <div class="mx-0 w-100 row"> <textarea id="notes" placeholder="Write here..." required="" class="form-control"> </textarea> </div>
<div class="mx-0 w-100 row">
<textarea id="notes" placeholder="Write here..." required="" class="form-control"> </textarea>
</div>
现在,如果不是我用的第一行
const notes = await driver.wait(until.elementLocated(By.id('notes')), delay)
或
显然,用notes
替换notes[0]
,它给了我
ElementNotInteractableError: element not interactable
问题是:为什么会发生这种情况?我不太喜欢选择带有数字的数组元素,但实际上我不得不这样做,我不明白为什么其他选择器不工作。这行代码
const notes = await driver.wait(until.elementsLocated(By.css('textarea')), delay)
…works as notes是由.css('textarea')标识为的所有元素的列表,这会导致elementsLocated()
出现一定的延迟,幸运的是,第一个匹配元素,即notes[0]
是您所需的元素,您已经完成了
显然,使用By.id('notes')
或By.xpath('/*[@id=“notes”]')
的第一个匹配元素不是您想要的元素
解决方案
最好的解决方案是按如下方式制作更精细的:
- css:
- xpath:
在第二种和第三种情况下,您是否仍在等待元素可见?是的,相同的代码!尝试使用const notes=wait driver.wait(直到.elementIsVisible(By.id('notes')),delay)
Wow,非常感谢,我对selenium是新手(我一周前开始使用),但我完全没有弄明白这一点。尽管如此,我们仍然不太清楚为什么使用By.id('notes')
的第一个匹配元素没有返回正确的元素,我的意思是,它是一个id,应该是唯一的@Diego在所需元素的顶部有其他元素,其属性为id
notes
,不可见,因此不是您所需的元素。
ElementNotInteractableError: element not interactable
const notes = await driver.wait(until.elementsLocated(By.css('textarea')), delay)
const notes = await driver.wait(until.elementLocated(By.css("textarea.form-control#notes")), delay)
const notes = await driver.wait(until.elementLocated(By.xpath("//textarea[@class='form-control' and @id='notes']")), delay)