Is there any way to pass a Python variable to a javascript script?

Asked

Viewed 45 times

0

I have a Javascript code that is triggered from another python code, in python I created a variable that pulls a json object of the database, and I need to consult this variable to know which report will update with Javascript code

const objeto = {
    "ticket1": ["Relatorio BI 1"],
    "ticket2": ["Relatorio BI 2"]
}

console.log('Clicker Injected');
const rows=document.querySelectorAll('#artifactContentList .row');
console.log(rows);
rows.forEach(row=>{
    objeto.chave1.forEach(valor =>{
        if(row.childNodes[4].innerText === valor){
            const button = row.children[1].firstChild.children[1]
            button.click()
    }
    })
});

Is there any way to take this variable in python and put it in Javascript, or, there is some way to link Javascript to the database postgresql

Below the call in python script code in java.

PS: The variable coming from the database is stored using the Pandas library

for n,w in workspaces.items():
        try:
            nav.driver.execute_script(clickerScript)
        except Exception as e:
            errH.report('Ocorreu erro na atualização do workspace:{n}'.format(n=n),e)
            nav.driver.quit()
  • It is a Python (Selenium) code that runs on script that goes to the Power BI http and updates reports at a certain time.

  • Includes a screenshot of the Python Java code call.

  • I want to add in a json object the names of the reports that have to be updated, and so do the update.

  • Do not use images for code prefer text as it facilitates copy for testing and page readers for visually impaired programmers can not interpret the image.

  • Modified.....

  • How can I do this?

  • Okay, but how do I get this value from the other side, inside the javascript?

  • For example in python define variavel_python = 10 , still a python script_string= f"let varpy = ${variavel_python};" when injecting the code contained in script_string in the javascript context there will be a variable varpy the value of which is 10;

  • Thank you very much Augusto, excellent help!

  • PS: to create multi-line fstring use three quotes ''' to open and to close. thus: teste = f''' texto de multiplas linhas '''

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.