元素无法相互作用的错误用于类型的输入元素TextBox Selenium JavaScript

发布于 2025-02-09 08:14:05 字数 1047 浏览 1 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2025-02-16 08:14:05

您正在使用XPATH作为输入的FindElement(by.classname()),因此将其更改为by.xpath()。
我不知道您要发送哪些键作为输入,但语法是:.sendkeys(“您在引号之间的文字”)

将最后一行更改为:

await driver.findElement(By.xpath('//input[@id="LocationSearch_input"]')).sendKeys("text");

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:

await driver.findElement(By.xpath('//input[@id="LocationSearch_input"]')).sendKeys("text");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文