I am using JS to find out the number of classes of certain type. When I execute JS in webdriver, it gives me the wrong value. When I execute the same JS on Firebug console, it gives me the right value.
Webdriver:
Returns 0
int Rects = ((IJavaScriptExecutor)driver).ExecuteScript<int>(" $('.optimization.flighting .interval').size()");
Returns 4
$('.optimization.flighting .interval').size()
Without more information on the page itself, those elements you are trying to target, version of jQuery etc etc, I can only guess at an answer (which I will happily delete if proven wrong), but I suspect it's being the JavascriptExecutor needs a little more pushing when returning values:
int Rects = ((IJavaScriptExecutor)driver).ExecuteScript<int>("return $('.optimization.flighting .interval').size()");
Specifically, adding the return statement before the actual Javascript, in a similar way you can add return to C# methods.
(Also, why you are not using the built in element finding mechanisms?)
Related
How to make Execute JavaScript of RobotFramework return a value
I need the text of a class element be returned using javascript code to a variable within RF. I have tried the following code but I get ${search_result} = None. It's not returning the text within the element selected.
:FOR ${index} IN RANGE 0 23
\ ${search_result} = Execute JavaScript floatMaster.querySelectorAll('#gContact_searcResultshWindow .gContactDetailClientListDiv .gContactListName')[${index}].innerText
I have also tried below but I just get JavascriptException: Message: javascript error: Unexpected number
${search_result} = Execute JavaScript function myFunction(${index}) {return floatMaster.querySelectorAll('#gContact_searcResultshWindow .gContactDetailClientListDiv .gContactListName')[${index}].innerText}()
The reason of the for loop is that the javascript locator has multiple results and I want to make sure each of them carry a specific text. I have a "should contain" command at the end of the loop.
When I try the javascript locator command with the index in browser console, I get a value just fine.
I am also aware of the Execute Async JavaScript command but it seems complicated and I don't understand how it works. Somebody help please.
Try:
${search_result}= Execute JavaScript return floatMaster.querySelectorAll('#gContact_searcResultshWindow .gContactDetailClientListDiv .gContactListName')[${index}].innerText
I am currently working on a project where I need to integrate the use of the Selenium Webdriver. I am using the Chrome implementation of Web Driver and running it via Javascript. I am currently testing a simple quantity input form. I am having trouble with a particular aspect of this project and that is ... I need the test to run through the form and put in different values everytime. I am placing the values via the sendKeys function. Now the trouble starts here... I need to grab the value that the sendKeys function inputs into the field and console.log a message depending on the value.
If the value is over a 100 I need the test to console.log the message "Exceeds 100".
If the value is less than 0 I need it to console.log the message "Below 0".
And if there is no value I need it to console.log the message "No input".
It runs through and puts in new values just fine. But the issue has been grabbing the value and console.logging a message depending on the value. I've tried many different options but there's just so little documentation related to this exact topic. I will link my code below, and I appreciate any input you guys may have... because it has me stumped unfortunately.
Also I am curious if this can be done using assertions in any way...
Test File Below:
https://gist.github.com/anonymous/89a84dbc15ba4088719400be1f359045
There is a method getAttribute(String attrName) it will accept a string parameter, pass attribute name against which value got set.
for example:
WebElement element =driver.findElement("your unique element locator");
String valueText=element.getAttribute("value");
about the answer above me - you should try adding a .getText(), So the attribute value would become a String.
WebElement element = driver.findElement("your unique element locator");
String valueText = element.getAttribute("value").getText();
Please add the full error message, A screenshot of the console would be good.
I'm using JavaScript executor via Selenium to retrieve a web element by Point as follows:
element = (WebElement) driver.executeScript("return document.elementFromPoint(arguments[0], arguments[1])",
location.x, location.y); // x = 482 y = 539
Although I am providing the correct coordinates of the web element, Javascript seems to be returning much more than just the element I ask for.
When I attempt to print the element text using System.out.println(element.getText()); it prints too much information.
For example, if you navigate to this page via Selenium Map: Defining a Method for Type Integer and Double but not String I am able to retrieve the x y coordinates of the element defined by the XPath: .//*[#id='question']/table/tbody/tr[1]/td[2]/div/div[1]/p[1].
When I perform the method System.out.println(element.getText()); on the original web element (not the one returned by JS) this results in output of:
I'm attempting to define a method putIfGreaterThan() for my new Map class (given a key it replaces the old value with the new value only if the new value is greater than the old value).
However, when I pass the coordinates via JavaScript executor the element returned begins from:
I'm attempting to define.....
All the way through:
You can do some stuff with ? extends Number or something like that; I'll upvote the answerer. Gotta bug to fix right now, so can't tend to this. – Bathsheba yesterday
Can't you use Number since Double and Integer extend from Number? – Abubakkar yesterday
add a comment
This is not the original element (it encompasses much more than the original element).
Why is it returning the entire element as appose to a section of the element? I am sure the coordinates are correct.
I am still new a Javascript tried earching and tried the development tool in Chrome, too see if I could find the problem.
Working in Intellij IDEA 13, Java, javascript and xhtml.
My problem is that I have a piece of javascript, then in IDEA when moused over, says that
Expression Expected
the javascript code looks the following
<script type="text/javascript>
function nameOfFunction(){
if(#{trendAnalysisLocationReportController.model.showTargetLine}){
this.cfg.series[this.cfg.data.length-1].pointLabels = {
show: false
};
}
}
<\script>
the method in the if sentence is a java method with a boolean return value.
the error is shown when hovering
'#{'
if Had a look at the following questions, before :
Expected Expression
boolean in an if statement
But didnt get me a solution.
what Iam I doing wrong ?
It looks as though the problem is the part that you've got within the #{...} block. Without knowing the context, it's hard to be sure, but is this something in a view/JSP page that's supposed to be replaced with a property at runtime? The if block will expect the part inside the brackets to be a boolean value, so if that's rendered to either 'true' or 'false' it would execute at runtime but would likely show the error you're seeing in your IDE as it's not actually a valid piece of JavaScript. If, on the other hand, you're expecting to be able to call your Java method/property from your JavaScript code, you're going to need to do something that requests that value from the server-side code - AJAX or similar.
Also worth noting that we can't see what this.cfg is supposed to represent. If that's your entire script block, then there's nothing that defines the cfg object within the current scope.
One last thing, you should change the <\script> end element to as it won't be understood properly by some browsers.
This question pertains as much to the ECMAScript language implementation we know as JavaScript as it does to jQuery and the developer tools availble in most popular browsers.
When you execute a statement like so:
var theElement = $('#theId').closest();
what is the type of theElement?
I assume that in a jQuery situation like above, many jQuery methods including the one above actually return the jQuery object itself, which packages the stuff you actually want to get to. This, so that it may maintain a fluent API and let you join method calls in a single statement like so:
$('#selector').foo().bar().gar().har();
However, in the case of jQuery then, how do you determine what the real underlying type is? For example, if the element returned was a table row with the Id tableRowNumber25, how do you get to that, say, using FireBug.
When I look at either a jQuery returned object or a simple JavaScript object in the watches window of Firebug or any of the Developer Tools in most popular browsers, I see a long laundry list of properties/keys and I don't know which one to look at. In a jQuery object, most of the properties are lamdas.
So, really, my question is -- how do you know the underlying type, how do you know what's actually being returned?
The type of theElement will be [object jQuery].
If you want the HTML element itself, you have to select it:
console.log(theElement[0]) //Return <div id='theId'>
console.log(theElement.get(0)) //Return <div id='theId'>
If you want the node name, there is a property in the HTML node element call nodeName wich return the capitalised node name:
console.log(theElement[0].nodeName)// Return DIV
typeof(jQueryElementList.get(0)) will return a string of the type.
Some browsers might return this as upper or lower case, I think. IE probably uppercases (see Testing the type of a DOM element in JavaScript). Apparently you can check the nodeType attribute (jQueryElementList.get(0).nodeType) to determine whether it is an html object/tag.