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.

Bellerofont
  • 1,081
  • 18
  • 17
  • 16
bhansa
  • 7,282
  • 3
  • 30
  • 55

2 Answers2

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"
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thanks Guys, also is there any method to access console output ? – bhansa Jan 31 '17 at 13:37
  • You can try to check this http://stackoverflow.com/questions/15462122/assign-console-log-value-to-a-variable, but I didn't try it personally, so I don't know whether it could be applicable in your case – Andersson Jan 31 '17 at 13:42
0

You can try following way to get an alert text

alert = driver.switch_to_alert()
alert_text = alert.text
NarendraR
  • 7,577
  • 10
  • 44
  • 82