This question already has answers here:
How to open multiple webpages in separate tabs within a browser using selenium-webdriver and python
(1 answer)
What is the fastest way to open urls in new tabs via Selenium - Python?
(2 answers)
Closed 3 years ago.
I have this piece of code:
((JavascriptExecutor)driver).executeScript("Object.assign(document.createElement('a'), { target: '_blank', href: 'https://facebook.com'}).click()");
((JavascriptExecutor)driver).executeScript("window.open('https://google.com')'");
First command is meant to create a new tab and open facebook.com and it does, first is meant to open google.com but nothing happens, am I doing anything wrong?
Disclaimer:
1.I am not familiar with Javascript at all, this is a Java Selenium project (hence why the (JavascriptExecutor)driver).executeScript part and I need to use Javascript for these few lines.
2.I tried multiple simpler piece of codes instead of this one but nothing worked hence why I ended up with this which is not the simplest.
you have extra single quotation in your second line.
you should write ((JavascriptExecutor)driver).executeScript("window.open('https://google.com')");
Related
This question already has answers here:
JavaScript: Is there a way to get Chrome to break on all errors?
(5 answers)
Closed 6 years ago.
I'm trying to implement Metronic admin theme in my app, but when I click on a button, I get Empty string passed to getElementById(). and the buttons don't work.
I'm trying to track down which piece of code triggers this error. How can I find out where the problem is?
write debugger; right before the javascript code that you feel is troublesome. Also keep the inspect tools open (usually F12)
This will create a breakpoint in the code execution with which you can then debug your code and look for errors.
This question already has answers here:
execute a function after redirecting - javascript
(3 answers)
Closed 7 years ago.
So, on my website, a user types in a subject they want the gist of, then after searching, it redirects them to a Wikipedia API displaying the general idea of the subject. However, there's a bunch of information about the API that gets in the way on the webpage, so I need to use JavaScript to get rid of that excess stuff.
Unfortunately, after changing webpages, it seems I can't run any more code from my website.
Any solution to this?
You could use an iframe, like so:
<iframe id="wikipedia-stuff">
</iframe>
And populate it like so:
<script>
document.getElementById("wikipedia-stuff").src = "http://wikipedia.stuff";
</script>
The wikipedia stuff would be in the iframe, and your code would still be running.
This question already has answers here:
Android WebView always returns null for javascript getElementById on loadUrl
(3 answers)
Closed 5 years ago.
We have an app that uses a form for oauth, just for dev purposes I want to eliminate typing in the user name and password so I added some code like this for when the page finishes loading:
mWebView.loadUrl("javascript:document.getElementById('UserName').value='" + txtUser.getText()+"'");
But for some reason it doesn't fill in the form it makes a new page and just writes out the value of txtUser instead of filling in the input field? Why and how can I fix this?
If you look at a tool look Squirt it does an anonymous function call. So I am not that versed in javascript, but that anonymous call seems to be the key in that the browser sees it as the current page is making the call itself. So try this roughly:
mWebView.loadUrl("javascript:(function(){document.getElementById('UserName').value='" + txtUser.getText()+"';})()");
I faced a similar issue and since I couldn't find a proper solution, I used the following hack -
Fetch the html source text.
Replace </body> tag with </body><script>YOUR JS CODE</script>
webview.loadDataWithBaseURL(url, editedString, "text/html", "UTF-8", null);
Sorry for the hacky solution, but in case you can't find a solution, this could help :)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Debug Javascript
Every now and then i have one jquery object and i am not able to find whats inside it.
I had to try many combinations like see the class name , id and other attributes to see what is that variable and sometime by hiding it.
Is there any proper way of debugging the jquery object to see whats inside it?
Firebug, Internet Explorer Developer Toolbar, Chrome Developer Tools. These are some of the tools for some of the respective browsers.
Some IDES like eclipse let you debug inline.
Also there is a javascript statement : debugger;, which you can use for this purpose .
Using firebug or the chrome developer tools you can debug any javascript object.
You need to add a breakpoint just after the object initialization and then add the object to the watch panel.
Object members are displayed in a tree view.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to keep the browser history in sync when using Ajax?
I think this is a rather easy fix but I can't find an answer anywhere else...so here goes...I made this kind of template for my homepage...I know the code's not insanely elegant but my main problem is that what if I want to send someone to a specific part of my page...rather than just my "home"...take a look http://useless-r-us.t15.org/
I mean how can I reference each of "blag", "projects", and "about me" using some unique url but still have my pretty css3 transitions...I'm thinking something like this...
http://radokirov.com
P.S....I know blag is a typo --> http://xkcd.com/148/
You should use url hashes (the part of the url following the # sign).
Then, in javascript, in the ready (jquery) event handler, based on the url's hash, you should do the appropriate ajax request and populate the page with the appropriate content.
For more details: Modify Address Bar URL in AJAX App to Match Current State