Missing error after argument list - Selenium Python (executing Javascript) - javascript

I've been at this for a while now, I know there are some questions along the lines of this one but I cannot figure out where on earth my string isn't escaped correctly or needs more quotes.
I have this line
self.driver.execute_script("$x('//div[text()=\'Find Artwork\']')[0].click()")
and this works perfectly in the console
$x('//div[text()=\'Find Artwork\']')[0].click()
However when running in Python with Selenium + Chrome webdriver I get this error
selenium.common.exceptions.JavascriptException: Message: javascript error: missing ) after argument list
Unsure quite what to do here. I've tried double quotes, escaping, everything.
Thanks!

$x is not part of JavaScript, but just a utility in cosole of firefox and chrome.
See this question.
You can try something like this:
javaScript = "document.getElementsById('username')[0].click();"
driver.execute_script(javaScript)
//or
driver.execute_script("arguments[0].click()", element);

Related

IE11 + Edge 12-13-14: random JavaScript parsing errors, caused by very long object?

I'm getting reports of JavaScript errors in production (from TrackJS.com) from IE & Edge browsers like:
Expected identifier, string or number
Unterminated string constant
Expected ': ', '}' or ';'
illegal character
And when I check the error file / line, it's sometimes in "mathiasbynens/he" library (look on GitHub), like here: example 1
And sometimes it's in other parts of the code (like in YUI library). I've got lots of these and they seem completely random (targeting what seems to be valid code): example 2
The only idea I have would be that IE croaks when loading the very long JSON defined in the .js.
Have you ever had any issue of this kind?

Did this JavaScript break the console?

Just doing some JavaScript stuff in google chrome (don't want to try in other browsers for now, in case this is really doing real damage) and I'm not sure why this seemed to break my console.
>var x = "http://www.foo.bar/q?name=%%this%%";
<undefined
>x
After x (and enter) the console stops working... I restarted chrome and now when I do a simple
console.clear();
It's giving me
Console was cleared
And not clearing the console. Now in my scripts console.log's do not register and I'm wondering what is going on. 99% sure it has to do with the double percent signs (%%).
Anyone know what I did wrong or better yet, how to fix the console?
A bug report for this issue has been filed here.
Edit: Feeling pretty dumb, but I had Preserve log checked... That's why the console wasn't clearing.
As discussed in the comments, there are actually many different ways of constructing a string that causes this issue, and it is not necessary for there to be two percent signs in most cases.
http://example.com/%
http://%%%
http://ab%
http://%ab
http://%zz
However, it's not just the presence of a percent sign that breaks the Chrome console, as when we enter the following well-formed URL, the console continues to work properly and produces a clickable link.
http://ab%20cd
Additionally, the strings http://%, and http://%% will also print properly, since Chrome will not auto-link a URL-link string unless the http:// is followed by at least 3 characters.
From here I hypothesized that the issue must be in the process of linking a URL string in the console, likely in the process of decoding a malformed URL. I remembered that the JavaScript function decodeURI will throw an exception if given a malformed URL, and since Chrome's developer tools are largely written in JavaScript, could this be the issue that is evidently crashing the developer console?
To test this theory, I ran Chrome by the command link, to see if any errors were being logged.
Indeed, the same error you would see if you ran decodeURI on a malformed URL (i.e. decodeURI('http://example.com/%')) was being printed to the console:
[4810:1287:0107/164725:ERROR:CONSOLE(683)] "Uncaught URIError: URI malformed", source: chrome-devtools://devtools/bundled/devtools.js (683)
So, I opened the URL chrome-devtools://devtools/bundled/devtools.js in Chrome, and on line 683, I found the following.
{var parsedURL=new WebInspector.ParsedURL(decodeURI(url));var origin;var folderPath;var name;if(parsedURL.isValid){origin=parsedURL.scheme+"://"+parsedURL.host;if(parsedURL.port)
As we can see, decodeURI(url) is being called on the URL without any error checking, thus throwing the exception and crashing the developer console.
A real fix for this issue will come from adding error handling to the Chrome console code, but in the meantime, one way to avoid the issue would be to wrap the string in a complex data type like an array to prevent parsing when logging.
var x = "http://example.com/%";
console.log([x]);
Thankfully, the broken console issue does not persist once the tab is closed, and will not affect other tabs.
Update:
Apparently, the issue can persist across tabs and restarts if Preserve Log is checked. Uncheck this if you are having this issue.
Update 2:
As of Chrome 40, this issue is fixed.

QML Javascript "console.log" with UTF-8

As we all know that in QML Javascript we can log to console by calling the 'console.log' function.
However, as per my experience, I can't log UTF-8 strings to console that the strings are still in proper characters, some are replaced by question marks.
For example:
console.log("XXXaàáảãạYYY");
will become (in console):
XXX??????YYY
How to solve this problem? I know console is just for debugging so I can use latin, but anyway, full UTF-8 support would be more interesting.
My console show them just fine.
I guess the problem is not about QML, but your console can't handle those UTF-8 characters.

IE not loading page with Javascript and Raphael

I'm testing out a website that runs fine on Firefox (Win/Mac), Chrome (Win/Mac) and Safari. I'm having difficulty with Internet Explorer unfortunately. I get the following error message:
SCRIPT65535: Unexpected call to method or property access.
raphael-min.js, line 8 character 64961
I've taken a look at the debug output which looks like just takes me to a part of the Raphel library:
c=a.getScreenCTM()||a.createSVGMatrix()
I've searched for this error message online, but I don't understand what solution is relevant to this case as I've no idea what is causing the problem. I am also using the jQuery library. Are there any tests that I can do that can give me more information about the source of the problem?
I just found how to patch this, in order to keep the compressed version of Raphael.
Replace (don't forget the coma):
c=a.getScreenCTM()||a.createSVGMatrix(),
By that (dont't forget the end space):
c;try{c=a.getScreenCTM()||a.createSVGMatrix()}catch(e){c=a.createSVGMatrix()};var
Works fine ! :)
Means :
c; : declaration of variable c, and stop the first instruction.
try{c=a.getScreenCTM()||a.createSVGMatrix()}catch(e){c=a.createSVGMatrix()}; : our instruction, surrounded by a try/catch, to avoid IE error
var + a space : (don't forget the space!) allow us to continue to declare variable
I found out that it's an issue with compression (of the js file). I had the exact same issue and I had been searching for a solution. Guess what? I tried it with the uncompressed Raphael file and voila! No more issues. Compressed file needs a tweak, it seems.

Running CoffeeKup on Rhino?

I'm trying to get CoffeeKup to work with Mozilla's Rhino engine. Not much luck so far. I'm trying this simple template:
templates ?= {}
templates.first = ->
doctype 5
html ->
head ->
title "#{#title}"
body ->
h1 "#{#hello}"
Which I compile into Javascript and then try to render using
CoffeeKup.render(templates.first, {title: 'Say Hello', hello: 'Hello World!'});
But it fails with this:
org.mozilla.javascript.EcmaError: SyntaxError: invalid return (CoffeeKup#304(Function)#230)
In the Javascript version of coffeekup.coffee, line 304 is this one:
return new Function('data', code);
And code line 230 (last line) is this:
).call(data);return __ck.buffer.join('');
Does anything look out of the ordinary or is this perhaps a Rhino bug?
If it works on Node/V8 + browsers but not Rhino, you can be fairly certain it's something specific to Rhino (not necessarily wrong, though). In the longer comment at the top of coffeekup.coffee it says that it will run on Node or on browsers, so that's most likely the case.
To really know what's going wrong, you need a debugger where you can step through the code (I don't know how to do that on Rhino, possibly via Eclipse or maybe even jdb if you have all sources at hand and are very Java proficient). If you find something reasonable trivial, try to file an issue, but CoffeeKup doesn't seem to have seen much activity for the last months. If you're up for a challenge, fork, fix and pull request.

Categories