元素无法相互作用的错误用于类型的输入元素TextBox Selenium JavaScript
我正在尝试使用Selenium将位置输入到Weather.com的搜索选项卡中。我最初在Python中编写了代码,它正常工作:
// this works
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.weather.com/')
time.sleep(2)
elem = driver.find_element_by_id('LocationSearch_input')
time.sleep(2)
elem.send_keys(location)
time.sleep(2)
elem.send_keys(Keys.ENTER)
现在,我正在尝试将此代码转换为JavaScript,但是我一直遇到一个错误“ element notintactableError:element不可交互”。我很困惑为什么会发生这种情况,因为相同的ID标签在我的Python代码中起作用,并且当我检查网页时,使用该ID的ID是类型的TextBox。
await driver.get('http://www.weather.com/');
console.log("step0");
await driver.findElement(By.xpath('//input[@id="LocationSearch_input"]'));
console.log("step1");
await driver.findElement(By.xpath('//input[@id="LocationSearch_input"]')).click();
console.log("step2");
await driver.findElement(By.className('//input[@id="LocationSearch_input"]')).sendKeys(location, Key.RETURN);
console.log("step3");
上面的代码贯穿步骤2,并将错误在console.log(“ step3”)上扔到线上。如何将工作的Python代码正确地转换为JS?
I'm trying to use selenium to input a location into the search tab of weather.com. I initially wrote the code in Python and it's working as expected:
// this works
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.weather.com/')
time.sleep(2)
elem = driver.find_element_by_id('LocationSearch_input')
time.sleep(2)
elem.send_keys(location)
time.sleep(2)
elem.send_keys(Keys.ENTER)
Now, I'm trying to translate this code into Javascript, but I keep getting an error "ElementNotInteractableError: element not interactable." I'm confused why this is happening since the same id tag works in my Python code, and when I inspect the webpage the element with that id is of type textbox.
await driver.get('http://www.weather.com/');
console.log("step0");
await driver.findElement(By.xpath('//input[@id="LocationSearch_input"]'));
console.log("step1");
await driver.findElement(By.xpath('//input[@id="LocationSearch_input"]')).click();
console.log("step2");
await driver.findElement(By.className('//input[@id="LocationSearch_input"]')).sendKeys(location, Key.RETURN);
console.log("step3");
The above code runs through step 2 and throws the error on the line before console.log("step3"). How can I properly translate the working Python code to JS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用XPATH作为输入的FindElement(by.classname()),因此将其更改为by.xpath()。
我不知道您要发送哪些键作为输入,但语法是:.sendkeys(“您在引号之间的文字”)
将最后一行更改为:
You're using findElement(By.className()) with an xpath as input so change that to By.xpath().
I don't know exactly what keys you're trying to send as input but the syntax is: .sendkeys("Your text here between quotes")
Change the last line to: