0

I am new in selenium. What I want to do is to execute javascript function through python. And use the value returned by javascript function. I am stuck with above error. Please help me. This is my js code:

var doc = ""
var p = document.getElementsByTagName("p")
var s = p[0].parentElement
for (var i=0; i<s.childElementCount;i++)
{
  var a = s.children[i]
  scrap(a)
}

result()

function scrap(a)
{
  if (a.tagName == "H1")
    doc = doc +a.innerText
  else if (a.tagName == "H2") {
    doc = doc +a.innerText
  }
  else if (a.tagName == "H2") {
    doc = doc +a.innerText
  }
  else if (a.tagName == "H3") {
    doc = doc +a.innerText
  }
  else if (a.tagName == "H4") {
    doc = doc +a.innerText
  }
  else if (a.tagName == "P") {
    var j = a.childElementCount
 

      doc = doc +a.innerText

  }
  else if (a.tagName == "img") {
    doc = doc +a.innerText
  }
  else if (a.tagName == "li") {
    doc = doc +a.innerText
  }
  doc = doc + "\n"
}

function result()
{
  return "doc"
}

And this the python script:

js = open("generalized.js", "r").readlines()   // js code
driver = webdriver.Firefox()
result = driver.execute_script(js) // string returned by js function is saved in result
doc = open(path,"w")
doc.write(result)

2 Answers 2

1

readlines() returns list, driver.execute_script expects a single string. Try to read the file as a string

js = open("generalized.js", "r").read()
Sign up to request clarification or add additional context in comments.

Comments

0
js = open("generalized.js", "r").readlines() 

This is returning a list of strings not a string.

Try to join the result to a string:

result = driver.execute_script("".join(js))

or read as string:

js = open("generalized.js", "r").read()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.