Search code examples
javascriptautomation

JavaScript. Help copying text from x.path and inserting into a popup text box


I am trying to create a script to insert a sentence into a pop-up box. The sentence will remain the same, with only the person's name changing.

I select a check box on the webpage, copy the person's name from the web page using the ClassName (also tried xPath), select the upload button and finally, enter a sentence with the person's name inserted. Everything works fine until I insert the code to copy the name.

I activate the script from a bookmark in firefox as this will be the first of many scripts, and it is in a convenient place when working.

I thought this would be simple, but it is causing me some problems. Any help on this would be greatly appreciated.

document.getElementById("1234").click();
NAME = document
    .getElementsByClassName("CLASS_NAME");
    .getText()
    .then(function (value) {
        return value;
    });
document.getElementById("UploadBoxButton").click();
document.getElementById("Notes").value = "Hello " + NAME + ". How are you?";
document.getElementById("Notes").click();

Solved:

async function example() {
    let NAME = document.querySelector("CSS PATH").textContent;
    let CANDIDATE = NAME.trim()
    document.getElementById("ELEMENTID").click();
    document.getElementById("ELEMENTID").click();
    document.getElementById("ELEMENTID").value = "Hello " + CANDIDATE;
}
example()

Solution

  • What is the problem exactly ? NAME hasn't the expected value ? I think this is due to the use of async function. I mean, when NAME is called to set Notes value, getText() has probably not finished to be call asynchronously.
    Moreover, where does getText() come from ? Can't you simply use :

    document.querySelector("#1234").click();
    const NAME = document.querySelector(".CLASS_NAME").textContent;
    document.querySelector("#UploadBoxButton").click();
    document.querySelector("#Notes").value = "Hello " + NAME + ". How are you?";
    document.querySelector("#Notes").click();
    

    And what is Notes ? Are you sure you can change its value like this ? Otherwise, try this :

    document.querySelector("#Notes").textContent = "Hello " + NAME + ". How are you?";