Is there a way to store in a variable the text in a certain element in selenium? - selenium

I have a selenium script that I created with Selenium IDE. I was
wondering if there is anyway to store the text that is in a certain
element in a variable so that I can use that text later in the script? I am using selenium IDE to do it and then importing it into browsermob.
For Example: I have this html:
<div id=title>
<h2>Website</h2>
<h3><span>web app</span>www.google.com</h3>
</div>
The text in the h3 (www.google.com) changes with different pages. I want a script to be able to run on all these pages that grabs the text in the h3 (in this case www.google.com), and stores it in a javascript variable which I can use in a later part of the script.

http://seleniumhq.org/docs/04_selenese_commands.html#store-commands-and-selenium-variables
Depending what exactly you are trying to do, the storeEval command may be of use. You should just be able to start the Xpath with // as the argument to the storeEval command (so that Selenium knows that you are referencing an Xpath rather than, say, a DOM element).

see How do you store an elements text with selenium in a javascript variable?

You can use the following piece of code for your task. It will work.
storeEval window.document.getElementById('menuless') varname
echo varname

Related

How to select an element from Span tag using xpath or css selector

I'm trying to automate process of pinging a person on a messenger on selecting the messenger name(Nayan)
Please find the Html code
<div class="Presence__text">
<span class="Presence__nameText">Nayan<span class="Presence__contactFlagIndicator"></span>
as text element present inside the span tag is unique (Nayan), i want to select based on that text element.
Problem statement :Unable to select an element using text present in span tag
I wanted to open the text of "Nayan" using xpath, can anyone help me solving this problem please.
XPath can be used to locate elements based on their text content.
Accordingly to presented here HTML the following XPath can be used:
"//span[contains(text(),'Nayan')]"
Selenium command using this XPath in Java (you didn't mention the language you are using, but accordingly to your previous questions I see you are using Java) can be as following:
driver.findElement(By.xpath("//span[contains(text(),'Nayan')]"));
In case "Nayan" text is unique it's always better to use contains rather to use equals method since web element may contain extra spaces.
I mean when possible [contains(text(),'Nayan')] is better to use than [text()='Nayan']
Also, since you are using Selenium it can be Xpath 1.0 only since Selenium not supporting Xpath 2.0 and higher, it supports XPath 1.0 only

Does selenium IDE have a way to handle dynamic elements?

I was using selenium IDE to automate testing on a web page that has dynamic xpath in it.
I noticed selenium IDE was capturing the xpath fine the first time playing it. Then after closing the browser and opening, of course the xpath has changed, but the target saved was the old xpath.
Is there a way to handle this in selenium?
I know I can use the .contains method but can i apply that to the target?
Picture of selenium IDE firefox extension
To identify the dynamic elements you can construct dynamic locators. As couple of examples:
Using a css for a <span> tag with id attribute starting with abc:
span[id^='abc']
Using a css for a <span> tag with class attribute containing pqr:
span[class*='pqr']
Using a xpath for a <span> tag with value attribute ending with xyz:
span[value$='xyz']
Using a xpath for a <span> tag with id attribute starting with abc:
//span[starts-with(#id, 'abc')]
Using a xpath for a <span> tag with class attribute containing pqr:
//span[contains(#class, 'pqr')]
Explanation of the dynamic CSS_SELECTOR
The wildcards are defined as follows:
^ : To indicate an attribute value starts with
* : To indicate an attribute value contains
$ : To indicate an attribute value ends with
References
You can find a couple of relevant detailed discussions in:
Java Selenium webdriver expression finding dynamic element by ccs that starts with and ends with

Extracting A Value From An Inline JS Script Tag With RobotFramework

There is some inline JS that runs at the top our website. It contains some name value pairs that it pulls from the webpage. I am writing tests with robot framework to assert these exist and have values. A small extract of the JS looks as follows:
siteId':'1133','offerId':'1228','productId':'549','
I am able to assert the existence of the names using the xpath locator as follows
page should contain
element xpath=/html/head/script[contains(text(),"siteId")]
With robot framework and Selenium Library I am asserting that the names exist using the page should contain element keyword and this works perfectly.
Now I need to extract the value, so '1133' in the example above.
I have been using the get element attribute keyword which has worked well when grabbing values in other places in the DOM. But doesn't seem to work for this case. I keep getting none as the value.
My xpath probably needs to written differently for me to be able to extract the value from it, but i'm not sure.
All help or ideas are greatly appreciated.
You should be able to get the contents of the script tag by fetching the value of the innerHTML attribute. reference
I am not familiar with robot framework, but based on the info you provided, I suspect using get element attribute with innerHTML will return the script tag's contents as a String, which you could then match using a regular expression.

Selenium object identification

I am using Selenium webdriver to test my application & i am facing difficulties in identifiying button on the same. the code snippet is like :
<input type="submit" onclick="return sign(this);" value="Login">
and its xpath is :
html/body/table/tbody/tr[2]/td/center/form/center/table/tbody/tr[3]/td/center/input[1]
Which object property to use and how?
You should not use that XPath.
I would hazard a guess that you used some sort of tool, whether it's Firebug or IDE, to generate that XPath. Stop that now!
XPath is fine to use, and can be used here, just not relying on the tools to generate it for you! That XPath is destined for failure!
You will need to provide more HTML, specifically around that button.
However, you should just be able to use something as simple as:
//input[#value='Login']
You can use the xpath, if that is really stable. I found that it is much easier to define id tags in the html elements and the use a By.id locator. Alternatively you can use css selectors, depending on the "uniqueness" of your button something like this could work:
By.cssSelector("input[value='Login']")

Storing values in Selenium IDE with a non standard tag

I have a non standard tag in an HTML doc that I need to write a selenium test for.
Here is the tag: <evo:password>SomeText</evo:password>
And my selenium ide command I'm trying:
Command: storeEval
Target: xpath=/x:html/x:body/x:div/x:div[1]/x:div[2]/x:div/x:div/x:table/x:tbody/x:tr[2]/x:td[2]/x:strong/x:evo:password
Value: adminPass
Not sure what I need as my Target to get this to work and store the value between my tags.
I had to get creative with my selector. I ended up using a CSS selector and getting the last element with it.
css=strong:last
I'm sure you can also use :nth-child among other natural css selectors.