0

My aim is to access js variable for a particular webpage and I want to print it in terminal/cmd for further process such as writing the Output into a file.

from selenium import webdriver
with open("list_links.txt","rb") as fs:
    data = fs.readlines()
    for line in data:
        print "checking for: ",line 
        baseurl = line
        driver = webdriver.Chrome()
        driver.set_window_size(140, 800)
        driver.get(baseurl)
        driver.execute_script("alert(someVariable)")


where txt file consists for different links and I am opening every page and popping an alert box which is giving me the output in the browser.

Everything is Working Fine, I just want to read/access the output which I am getting in the alert box.

SO, Is there any way to do this?

Also if there any way to read the same from console.log output text, please share.

2 Answers 2

1

You can get value of someVariable without reading text content of alerts as follow:

var_text = driver.execute_script("""
                                 var someVariable = "someValue";
                                 return someVariable;
                                 """)

print(var_text) # Output- "someValue"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Guys, also is there any method to access console output ?
You can try to check this stackoverflow.com/questions/15462122/…, but I didn't try it personally, so I don't know whether it could be applicable in your case
0

You can try following way to get an alert text

alert = driver.switch_to_alert()
alert_text = alert.text

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.