When using StoreValue, Selenium claims "This element has no value" - css

I have the following code, where I need to get the value:
<a href="/checkout/cart.jsf">
Items:
<span class="numberofitems">1</span>
</a>
However, when I attempt to point at it using css=span [class='numberofitems'], I get the message "The element has no value; is it really a form field?" The element does have a value which I can see in Firebug, but I can't figure out how to properly store this value.

Here we go.. every WebElements given below will give better outcome to get the value '1'.
By.xpath("//a[#href='/checkout/cart.jsf']/span")
By.xpath("//a[text()='Items: ']/span"))
By.xpath("//span[#class='numberofitems']")
If they are not working,
Check whether the Elements are HIDDEN with help of Firebug.
Check whether the classname, 'numberofitems' is repeated.

Related

Extract number data from HTML with RobotFramework

I need to extract a number from an HTML page and convert it into a variable in my test case.
The problem is that there is no ID directly to this element, here is the HTML code, I want to get the 54 (that number can change that's why I need to identificate him with another way), I tried Get Text by using "resultat" but I get "54 ligne(s) trouvée(s)" but I only want "54":
<div class="tab-interpage> == $0
<div class="resultat">
<b>54</b>
ligne(s) trouvée(s)
</div>
...
You have other options how to locate an element, see Locating elements section in Selenium Library.
This might be a situation that requires xPath, I can imagine this one works (but I don't see the whole DOM, so I can't be 100 % sure):
//div[#class="resultat"]/b
combined with the keyword:
${var}= Get Text //div[#class="resultat"]/b
Obviously if there're more div elements with class "resultat", you might run into problems here. In this case, explore the DOM a bit more and see what are some other ways you can get to the element you need.
I think it'd be much more readable if the HTML elements had proper attributes like:
form with class attribute
unique ids usually work best

How can I find classname an element with multiple classes in cypress

I'm a tester, and I have this HTML code.
<div id="columns">
<div class="column ce-editable c1">
</div>
</div>
I'm using Cypress. I want to examine if there is 'c1' class name found in the div element. If I try:
cy.xpath ('//div[#id="columns"]//div')
.should('have.class', 'c1')
I have this error:
CypressError: Timed out retrying: You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object.
The chai-jQuery assertion you used was:
> class
The invalid subject you asserted on was:
> [<div.column.ce-editable.c1>]
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.
May I give you a whole other direction. It goes far, far away from using Xpaths :). What you can do to archive what you want is the following:
cy.get('#columns')
.find('div')
.should('have.class','c1')
This results in a few positive things:
1. It's easier to read what is happening
2. Debugging is easier, you'll see that Cypress tries to verify c1 in three steps. First find some ID with 'columns', than find a div below that and finish with asserting that the last searched div has a class called 'c1'.
3. The results show more neath since should() is actual an assertion instead of an action.

How to uniquely identify elements with selenium for below Expedia site example

Lets say I go to - https://www.expedia.co.uk/
I go to Trains Tab and do a search for any date with 1 passenger selected.
You'll be on next page where to select trains from a list, now if I want to click on any ShowFares button its not being recognized uniquely by CSS= .btn-secondary.btn-action (its returning more than one matching node. So couldn't use it.
while using xpath -
.//*[#id='ember968']/div/div[1]/div[2]/div[2]/div/button
I see its recording #id with some emberxxx which again is not unique as its getting changed for every other search list..
Similarly when I clicked on ShowFare then unable to pick a train or fare as same above problem occurring as CSS is returning several nodes and xpath has this emberxxx which is not unique.
As with the same attributes, we have more than one element we are not able to pick right one. I tried with jquery selector .btn-secondary.btn-action:eq(1) and it is working. By using above selector you will pick first Show Fares button every time Let me know if you have any queries.
CSS Selector: .flex-1up.flex-listing.flex-theme-light li:nth-child(1) button
Use, for example, xpath-function starts-with:
(//*[starts-with(#id, 'ember')])[2]
This function find part of name. And then you can use filtr by [] to find needed element by index.
I see there are lot other elements having same xpath. Here is my suggestion if you want to click on first element.
//button[#class='btn-secondary btn-action']/span - push that to list, loop through list and use getText(). If matches 'Show Fares', click on that.
List<WebElement> buttonList = driver.findElements(By.xpath("//button[#class='btn-secondary btn-action']/span"));
for(int i=0; i<=buttonList.size() ; i++){
WebElement ele = buttonList.get(i);
if(ele.getText().contains("Show Fares"){
ele.click();
}
}

Get Element Attribute in robot framework

hi how to use Get Element Attribute in Robot framework? in instruction I have
Return value of element attribute.
attribute_locator consists of element locator followed by an # sign and attribute name, for example element_id#class.
I have this xpath=${check_radio_xpath}#class is this right way?
where ${check_radio_xpath} = md-radio-11
I get this error:
${ischecked} = Selenium2Library . Get Element Attribute xpath=${check_radio_xpath}#class
Documentation:
Return value of element attribute.
TRACE Arguments: [ 'xpath=md-radio-11#class' ]
DEBUG Finished Request
FAIL ValueError: Element 'xpath=md-radio-11' not found.
I think you're pretty close. Please try to format your question better, I took a quick shot because your question is difficult to read. The result will be more and better help from the community
${RADIO_XPATH} //*[#id="${check_radio_xpath}"]
${CLASS}= Selenium2Library.Get Element Attribute ${check_radio_xpath}#class
sample for this <div><label for="foo"></label></div>
${for_value}= Get Element Attribute xpath=//div/label for
Log To Console ${for_value}
console result is:
foo
This snippet works for me :
Get Line Numbers And Verify
${line_number1}= Get Element Attribute //*[#id="file-keywords-txt-L1"] data-line-number
Log To Console ${line_number1}
${line_number2}= Get Element Attribute //*[#id="file-keywords-txt-L2"] data-line-number
Log To Console ${line_number2}
Verify in order of ${line_number1} and ${line_number2} is true
What was important is that the spaces/tabs between the keywords are correct, otherwise it does not get recognised as a so called keyword.
Thanks a lot, i wanted to check meta noindex content in page source.
i used this.
${content} Get Element Attribute xpath=//meta[#name="robots"]#content
should be equal as strings ${content} noindex,follow
You can use both XPath and CSS selector if you have selenium library
${title}= Get Element Attribute ${xpath} attribute=title

Polymer 1.0: Data binding variable to <iron-meta> element (value attribute)

In Polymer 1.0, I am trying to data bind a string variable {{str}} to an <iron-meta> element (tag in parent element) as follows.
This fails:
<iron-meta id="meta" key="info" value="{{str}}"></iron-meta>
The above code breaks. But the following code works (without the binding).
This works:
<iron-meta id="meta" key="info" value="foo/bar"></iron-meta>
The difference is the variable version {{str}} fails and the constant version "foo/bar" works.
Does anyone have a clue what is what is breaking the binding and how to fix it?
Edits in response to comment questions:
How does it fail? This fails silently. The values I have printed out simply do not update when I press the Login and Register buttons.
Here is a link to the code in a Github repository. See lines
You need to use an attribute binding and not a property binding
<input type="text" value$="{{str}}" />

Resources