0

I'm new to selenium, this is my first day of learning it so please go easy on me.

So I want to print umidTokenFromHeader a javascript variable of this website https://member.lazada.co.id/user/login. Here's my code so far

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://member.lazada.co.id/user/login")
# print (umidTokenFromHeader)
driver.close()

On the Console tab of Chrome Developer Tools, umidTokenFromHeader looks like this:

enter image description here

I found this question Reading JavaScript variables using Selenium WebDriver but I don't understand Java at all How do I do this on selenium with python ?

Joe
  • 791
  • 1
  • 9
  • 24
  • In selenium you can evaluate JavaScript using driver.execute_script, try this to access your variable and see if that works – rs007 Apr 08 '19 at 14:45
  • Can you provide the full code for that ? – Joe Apr 08 '19 at 14:52
  • sorry didnt notice your response, so you should just be able to get the value by doing this : ```driver.execute_script("return umidTokenFromHeader")``` and it should return the value for you. – rs007 Apr 08 '19 at 18:08

1 Answers1

0

You can use a JavaScript and execute_script() to return the value of umidTokenFromHeader variable. Then you can capture that value in element variable and finally print it.

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://member.lazada.co.id/user/login")
element = driver.execute_script("return umidTokenFromHeader")
print (element)
driver.close()
Alex Pas
  • 148
  • 1
  • 2
  • 7
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – CertainPerformance Apr 08 '19 at 23:32
  • Thank you, I'm using python 3 so it should be `print (element)` – Joe Apr 09 '19 at 05:31