Reading JavaScript variables using Selenium WebDriver

后端 未结 3 1104
小鲜肉
小鲜肉 2020-11-29 23:42

I\'m using Selenium WebDriver (Java) and TestNG to do some testing on a website I created. In this website, I also have JavaScript and in some of the functions, it returns v

相关标签:
3条回答
  • 2020-11-30 00:02

    All you have to do is:

    Object val = js.executeScript("return returnFoo();");
    

    That will give you what you are looking for.

    0 讨论(0)
  • 2020-11-30 00:05

    In Ruby you can use page.execute_script to evaluate a JavaScript variable (if it is accessable from the scope of the web browser). It looks like there is a similar method in Java here.

    Edit: This might be a use case that is more suited to a JavaScript unit testing framework such as Jasmine.

    0 讨论(0)
  • 2020-11-30 00:07

    No JavaScript functions need be defined. Nor is alert() needed.

    Object result = js.executeScript("return globalVar");
    

    For Python:

    result = driver.execute_script("return globalVar")
    
    0 讨论(0)
提交回复
热议问题