I'm learning to read javascript variable using Selenium WebDriver (latest version). Sometimes it works, sometimes not. Below is my try on whoscored.com and it keeps showing error
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://www.whoscored.com/Regions/81/Tournaments/3/Germany-Bundesliga");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
var tournament = wait.Until(ExpectedConditions.ElementExists(By.Id("tournament-fixture-wrapper")));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
var obj = (object)js.ExecuteScript("return window.allRegions;"); //always return error 'Additional information: Unable to cast object of type 'System.Int64' to type 'System.String'.
}
I think you should change
var obj = (object)js.ExecuteScript("return window.allRegions;");
to
List<object> list = js.ExecuteScript("return window.allRegions;") as List<object>;
since, return window.allRegions; does not return a string but array of objects.
Edit
Just went through the page and looks like window.allRegions returns a List of json objects. And, does feel like creating a list of json object can be unwanted overwhelming of programming. I suggest you to narrow down the goal either with modifying the javascript or performing some filtering like following.
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
var tournament = wait.Until(ExpectedConditions.ElementExists(By.Id("tournament-fixture-wrapper")));
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
//getting count of regions
long count = (long)js.ExecuteScript("return window.allRegions.length;");
for (int i = 0; i < count; i++)
{
//grab the name of countries if that's what you wanted
string name = js.ExecuteScript("return window.allRegions[" + i + "].name;") as string;
Console.WriteLine(name);
}
Print:
Africa
Albania
Algeria
...
Zambia
Zimbabwe
Related
So with my limited JavaScript skills I have created a simple translate button. When you click the button it changes the text on the webpage you are on to either English or Chinese.
How would I go about making these changes stay when someone goes to different webpages or even refreshes the page? I have tried experimenting with the localStorage technique but to no luck.
Here is my JS code for the translate button:
// onclick behavior
$('.lang').click('touchstart', function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i) {
$(this).text(aLangKeys[lang][$(this).attr('key')]);
});
});
// preparing language file
var aLangKeys = new Array();
aLangKeys['en'] = new Array();
aLangKeys['ch'] = new Array();
aLangKeys['en']['home'] = 'Home';
aLangKeys['en']['about'] = 'About Us';
aLangKeys['en']['serv'] = 'Services';
aLangKeys['en']['sem'] = 'Search Engine Marketing';
aLangKeys['en']['webdev'] = 'Website Development';
aLangKeys['ch']['home'] = '首页';
aLangKeys['ch']['about'] = '关于我们';
aLangKeys['ch']['serv'] = '服务';
aLangKeys['ch']['sem'] = '谷歌与雅虎推广';
For local storage I have added in this right under the var = aLangKeys = new array.
localStorage.setItem("aLangKeys", aLangKeys);
var someVarName = localStorage.getItem("aLangKeys");
Thanks!
LocalStorage only allows storing strings so you need to use JSON.stringify() and JSON.parse() when getting and setting.
But first you shouldn't be using new Array() for non numeric keys. What you are really doing is making an array like object when you do that
Change to:
var aLangKeys = {
en :{},
ch: {}
};
// .... other definitions
To set
localStorage.setItem("aLangKeys", JSON.stringify(aLangKeys));
To get
var someVarName = JSON.parse(localStorage.getItem("aLangKeys"));
I'm working on an add-in for excel 2016 using the javascript API. I can successfully get the range into an array and get the values to show in console.log. I've also been able to get the values into a JSON array using JSON.stringify();
I need to manipulate the array to remove the empty values ("").
Can this be accomplished by using regular javascript array methods?
I'm thinking I can display the results back into a different worksheet using a similar approach like i did with var shWk
Here are some snippets of what I'm trying to do:
(function () {
"use strict";
// The initialize function must be run each time a new page is loaded
Office.initialize = function (reason) {
$(document).ready(function () {
app.initialize();
//document.getElementById("date").innerHTML = Date("MAR 30 2017");
$('#deleteTab').click(deleteTab);
$('#preview').click(preview);
$('#publish').click(publish);
});
};
function preview() {
Excel.run(function(ctx) {
//getting the colname from a date range in B2
var colName = ctx.workbook.worksheets.getItem('preview').getRange("B2");
colName.load('values');
return ctx.sync().then(function() {
//converting colname value to string for column name
var wkN = (colName.values).toString();
// displaying on the task pane
document.getElementById("tst").innerText = wkN;
// testing to confirm i got the correct colname
var shWk = ctx.workbook.worksheets.getItem('preview').getRange("B3");
shWk.values = colName.values;
//building the column connection by setting the table name located on a different worksheet
var tblName = 'PILOT_ZMRP1';
var tblWK = ctx.workbook.tables.getItem(tblName).columns.getItem(wkN);
//loading up tblWK
tblWK.load('values');
return ctx.sync().then(function(){
//this is where my question is:
var arry = tblWK.values;
for (var i=0; i < tblWK.length; i++){
if (tblWK.values !== ""){
arry.values[i][0]) = tblWK.values[i][0]
};
};
console.log(arry.length); //returns 185
console.log (arry.values);//returns undefined
tblWK.values = arry;
var tblWeek = tblWK.values;
console.log(tblWeek.length);//returns 185
console.log(tblWK.values);//returns [object Array] [Array[1],Array[2]
})
});
}).catch(function (error) {
console.log(error);
console.log("debug info: " + JSON.stringify(error.debugInfo));
});
}
What am I missing? Can you point me to some resources for javascript array handling in the specific context of office.js?
I want to thank everyone for the time spent looking at this question. This is my second question ever posted on Stack Overflow. I see that the question was not written as clear as it could've been. What i was trying to achieve was filtering out the values in a 1D array that had "". The data populating the array was from a column in a separate worksheet that had empty values (hence the "") and numeric values in it. the code below resolved my issue.
//using .filter()
var itm = tblWK.values;
function filt(itm){
return itm != "";
}
var arry = [];
var sht = [];
var j=0;
var s=0;
arry.values = tblWK.values.filter(filt);
//then to build the display range to show the values:
for (var i=0; i < itm.length-1; i++) {
if (tblWK.values[i][0]){
var arry; //tblWK.values.splice(i,0); -splice did not work, maybe my syntax was wrong?
console.log("this printed: "+tblWK.values[i][0]);
var cl = ('D'+i); //building the range for display
j++; //increasing the range
s=1;//setting the beignning range
var cll = cl.toString();//getRange() must be a string
console.log(cll);//testing the output
}
}
//using the variable from the for loop
var cl = ('D'+s+':D'+j);
var cll = cl.toString();
console.log(cll);//testing the build string
sht = ctx.workbook.worksheets.getItem('Preview').getRange(cll);
sht.values = arry.values; //displays on the preview tab
console.log (arry.values); //testing the output
The question was probably easier said by asking what vanilla javascript functions does office.js support. I found a lot help reading Building Office Add-ins using Office.js by Micheal Zlatkovsky and by reading the MDN documentation as well as the suggested answer posted here.
Regards,
J
I'm not sure what this check is trying to achieve: tblWK.values !== "". .values is a 2D array and won't ever be "".
For Excel, the value "" means that the cell is empty. In other words, if you want to clear a cell, you assign to "". null value assignment results in no-op.
You can just fetch the values form the array that contains null by using for each and can can push the null values into another array.
I imported json data into google scripts with:
var doc = Utilities.jsonParse(txt);
I can access most of the objects like such...
var date = doc.data1.dateTime;
var playerName = doc.data1.playerName;
var playerId = doc.data1.playerID;
var teamNumber = doc.data2.personal.team;
A bunch of objects I need to access have numbers as object names...
doc.data2.personal.team.87397394.otherdata
doc.data2.personal.team.87397395.otherdata
doc.data2.personal.team.87397396.otherdata
doc.data2.personal.team.87397397.otherdata
...but when I try to read the data with...
var teamId = doc.data2.personal.team.87397394;
... I get an error "Missing ; before statement."
I tried this...
var teamId = doc.data2.personal.team[87397394];
... and get "teamId undefined" in the log.
I also tied this with the same result...
var teamId = doc.data2.personal.team[+'6803761'];
I can read in the names as strings very easily with "For In", but can't get to the objects themselves. Every example I've found so far uses the brackets so I'm stumped what to try next.
Thank you!
Brian
UPDATE
I used this per your suggestions to get the object name into a variable and using the variable in brackets. No error but var test remains "undefined"...
for(var propertyName in doc.data2.personal.team) {
// propertyName is what you want
// you can get the value like this: myObject[propertyName]
Logger.log (propertyNames);
var test = doc.data2.personal.team[propertyName];
}
The log shows the object names, as expected...
87397394
87397395
87397396
87397397
I'm thinking it's a bug in Google's implementation. Here is an example if anyone wants to verify it. test will return undefined...
function myFunction1() {
var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
var doc = Utilities.jsonParse(txt);
for(var propertyName in doc.datablock_battle_result.vehicles) {
Logger.log (propertyName);
var test = doc.datablock_battle_result.vehicles[propertyName];
}
}
The problem seems to be in the Utitlies.jsonParse. The following works fine
var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
var doc = JSON.parse(txt);
for(var propertyName in doc.datablock_battle_result.vehicles) {
var vehicle = doc.datablock_battle_result.vehicles[propertyName];
Logger.log('Vehicle id is ' + propertyName);
Logger.log('Vehicle value is ' + JSON.stringify(vehicle));
break;
}
I've been developing a simple application using Adobe AIR, the HTML and javascript version.
The application submits a form to an online URL.
The values of the forms are JSON strings.
I'm using this function to submit the data:
function fetchStudents()
{
var stmt = new air.SQLStatement();
stmt.sqlConnection = conn;
stmt.text = "SELECT * FROM studentsTable2 WHERE deleted='0'";
stmt.addEventListener(air.SQLEvent.RESULT, function(event){
var result = event.target.getResult();
sync_students = JSON.stringify(result.data);
fetchCourses();
});
stmt.addEventListener(air.SQLErrorEvent.ERROR, errorHandler);
stmt.execute();
}
JSON.stringify works when I test the application in DREAMWEAVER by using: preview in ADOBE AIR.
sync_students is then a JSON string filled with all the data from the table correctly formatted.
But when I have created the AIR file and installed the application and run it, it no longer works.
sync_students is then a JSON string but it is completely empty... [{},{},{}]
I have read around a lot and seen suggestions to use JSON2.js etc and I have tried these but I haven't been successful.
This is driving me crazy, any help would be greatly appreciated.
Thanks so much in advance!
Have a look at the accepted answer of this question. The problem here was that garbage collection reclaims some var 'too soon' : in other words, the scope is baaad. :
AIR Sqlite: SQLEvent.RESULT not firing, but statement IS executing properly
EDIT : All this is a question of scope. You should look into 'javascript scope' and 'javascript closure' keywords to have a better understanding of this (if i may suggest).
below just a (very) short summary about ONE WAY to define a 'class' in javascript :
var MyNameSpace = {};
var MyNameSpace.SchoolDataSource = function() {
this.publicMember = 2;
this.publicFunction = function(x) {
var newValue = this.publicMember + _privateMemberOne + x;
return _privateFunctionMul2(newValue);
} ;
var _privateMemberOne = 1;
var _privateFunctionMul2 = function (y) { return 2*y; } ;
};
var mySchoolDataSource = new MyNameSpace.SchoolDataSource();
mySchoolDataSource.publicFunction(3); // ok. (returns (3+2+1) * 2 = 12)
var bar = mySchoolDataSource.publicMember; // ok. ( === 2)
mySchoolDataSource._privateFunctionMul2(4); does not work, which is what we want.
var foo= mySchoolDataSource._privateMemberOne; // does not work, which is what we want.
Within a javascript app, we're calling a sproc on SQL Server 2005 that's returning a resultset with 3 columns of type: bigint, varchar(20), and varchar(MAX).
When calling the sproc and reading the results, there's no exception being thrown. When enumerating the resultset:
var dbConn = DatabaseConnectionFactory.createDatabaseConnection("1","2","3","4");
var myResultSet = dbConn.executeCachedQuery("EXEC MySproc");
dbConn.close();
var myString = myResultSet.getString("MyVarcharMaxField");
When examining the value of myString, the value is not what I expect:
javax.sql.rowset.serial.SerialClob#15b4206
Any idea how to get the real value of the field?
Calling another method other than getString() maybe?
Varchar(MAX) is a CLOB (Character Large Object) data type. Here's some code you can use in order read its contents.
var clobField = myResultSet.getClob("MyVarcharMaxField");
var strOut = new java.lang.StringBuffer();
var aux = new java.lang.String();
// RTF data conversion
var kit = new Packages.javax.swing.text.rtf.RTFEditorKit();
var doc = new Packages.javax.swing.text.DefaultStyledDocument();
kit.read(clobField.getAsciiStream(), doc, 0);
var text = doc.getText(0, doc.getLength());
var myString = text.toString();