Search code examples
pythonweb-scrapingghost.py

Use python variables inside Ghost.py script


I'm trying to fill a form several times with Ghost.py, the values I need to input to the form are inside a list, the program looks like this:

inputData = ["input1", "input2", "andSoOn"]

ghost = Ghost()
with ghost.start():
    session = Session(ghost, download_images=False, display=True, user_agent=USER_AGENT)
    page,rs = session.open("http://somesite.org/fillme", timeout=120)
    assert page.http_status == 200

    # this is where i need the input data, to fill the values.
    session.evaluate("""
    document.querySelector('input[name="DataForm"]').value = "dataInput";
    """)

    # Do something and close
    session.webview.setHtml("")
    session.exit()

How do I pass the values from the inputData list to the querySelector method inside the script? I've been trying everything and so far no luck.

Running python 2.7, with Ghost 0.2.3 with PyQt4. Thanks in advance.


Solution

  • Did you try using a for loop?

    for input in inputData:
        session.evaluate('document.querySelector(\'input[name="DataForm"]\').value= "%s";' % input)