简体   繁体   English

如何从 Selenium 脚本调用外部 API 以使用 Node JS 填充字段?

[英]How do you call an external API from Selenium script to populate a field using Node JS?

I have a use case where I need to call an external API, parse the JSON that is returned and populate a form field in a web page all within a Selenium script written using Node JS. I have a use case where I need to call an external API, parse the JSON that is returned and populate a form field in a web page all within a Selenium script written using Node JS.

Something like this:像这样的东西:

// in Selenium script get the form field
let inputElement = await getElementById(driver, "my-id");
// then call an API including callback function

// in the callback function with the JSON response from the API
const myText = response.data.text;
await inputElement.sendKeys(myText,Key.ENTER);

I actually not even sure where to start with this - because I would be adding asynchronous code (the API call and waiting for the response in the callback) to the existing asynchronous code that is running as part of the Selenium script.实际上,我什至不确定从哪里开始 - 因为我将向作为 Selenium 脚本的一部分运行的现有异步代码添加异步代码(API 调用并等待回调中的响应)。 And I need to not lose references to the web driver and the input element.而且我不需要丢失对 web 驱动程序和输入元件的引用。

Some advice and recommendations to get me going would be very helpful.一些让我前进的建议和建议会非常有帮助。

If you are using node's inbuild https module the you can do something like this..如果您使用的是节点的内置 https 模块,您可以执行以下操作。

const { Builder, By, Key, until } = require("selenium-webdriver");
const https = require("https");

(async function example() {
  let driver = await new Builder().forBrowser("chrome").build();
  try {
    await driver.get("http://www.google.com/ncr");
    await https.get("https://jsonplaceholder.typicode.com/users/1", (resp) => {
      let data = "";
      resp.on("data", (chunk) => {
        data += chunk;
      });
      resp.on("end", async () => {
        // console.log(JSON.parse(data)["name"]);
        await driver
          .findElement(By.name("q"))
          .sendKeys(JSON.parse(data)["name"], Key.RETURN);
      });
    });
    await driver.wait(until.titleContains("- Google Search"), 1000);
  } finally {
    await driver.quit();
  }
})();

Or if you are already using library like axios, then you can do something like this或者,如果您已经在使用 axios 之类的库,那么您可以执行以下操作

const { Builder, By, Key, until } = require("selenium-webdriver");
const axios = require("axios");
(async function example() {
  let driver = await new Builder().forBrowser("chrome").build();
  try {
    await driver.get("http://www.google.com/ncr");
    const { data } = await axios.get(
      "https://jsonplaceholder.typicode.com/users/1"
    );
    await driver.findElement(By.name("q")).sendKeys(data["name"], Key.RETURN);
    await driver.wait(until.titleContains("- Google Search"), 1000);
  } finally {
    await driver.quit();
  }
})();

Hope this is what you are looking for..希望这就是你要找的..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM