0

Need help in retrieving values from script using python selenium. Few values are predefined and rest keeps on updating. Hope someone could help in retrieving the both. Tried few methods which gave no intended result.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import unittest
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
url = "https://demo.applitools.com/hackathonV2.html"
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)

class Test(unittest.TestCase):
    def test2(self):
      element = driver.find_element_by_id("username")
      element.send_keys("xxxx")
      element1 = driver.find_element_by_id("password") 
      element1.send_keys("k")
      element.send_keys(Keys.RETURN)

      element= driver.find_element_by_xpath('//*[@id="showExpensesChart"]')
      element.click()

      element= driver.find_element_by_xpath('//*[@id="addDataset"]')
      element.click()

      #need to get the global var values(months, barchartdata)
      c=driver.execute_script('return global')



if  __name__=="__main__":
    unittest.main()

Data to be retrieved : BarChartData,Data Updated values when "Show data for next year" is clicked.

enter image description here

5
  • it is variable in JavaScript so use JavaScript to get it. Commented Nov 30, 2019 at 8:08
  • yea. I tried by altering the attributes for" driver.execute_script("return document.getElementsByTagName('a')") ". But nothing seems to work. and in the script the function name is not defined as you can see in the image. So how what to use instead of " return document.getElementsByTagName('a') "? Commented Nov 30, 2019 at 8:10
  • For values which doesn't change you can get HTML as text and cut off from this text. If it has JSON format then you can use python's module json to convert to python's structure. Commented Nov 30, 2019 at 8:10
  • Why do you try to get tag <a> if value is in JavaScript. To get tag you can use driver.find_elements_by_tag_name('a') and later you can get text or attributes from this tag. Commented Nov 30, 2019 at 8:12
  • "document.getElementsByTagName('a') " this was just a try to understand how it works. Thant's all. So you are recommending to use scraping for this? or we can can do it just by using selenium? Commented Nov 30, 2019 at 8:14

1 Answer 1

1

Please find the below solution to get variable value from

labels = driver.execute_script('return barChartData.labels')
print labels



data = driver.execute_script('return barChartData.datasets[0].data')
print data

OutPut

[u'January', u'February', u'March', u'April', u'May', u'June', u'July']
[10, 20, 30, 40, 50, 60, 70]
Sign up to request clarification or add additional context in comments.

3 Comments

would you kind enough to accept my answer and hit up vote button once you verify answer from your end
I found out the same solution. Thank you for the great help. Btw could you please tell how to get the values for "barChartData.datasets[0].data" replacing 0 with i in the range (0,any number). I have tried but in JS i get only the last element.
upvoted. could you please help me with the above issue. for (let i = 0; i < 3; i++) { return (window.barChartData.datasets[i].label) } gives only last value for i=2. How to get the other values?

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.