Does selenium IDE have a way to handle dynamic elements? - selenium

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

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

Explain how selenium searches for an element

Hi I am looking for an explanation on how selenium searches for an element on a website. For us, we use inspect element to find the id, name, xpath, etc. of an element, and then put it in selenium for some action to be done. How does selenium find the element we tell it to find? Does it ctrl shift J like us and inspect element?
Note: I am not looking for how to code selenium to find element.
Html pages are just documents with elements structured like in a tree.
In general
Selenium uses element locators to find things. Locators work lazily. When you look up an element Selenium first checks if its cached. If not, it uses SearchContext which finds all elements within the current context (eg. DOM element) using a given mechanism, for example by XPathEvaluator.
SearchContext runs findElement() if you are looking for one element or findElements() if you are looking for more than one.
In simple terms, findElement() tries to run JavaScript script to find the element asynchronously. If it can’t, it tries to find it directly by using an interestingly called method – xpathWizardry, i.e. by using XPathEvaluator evaluation.
XPath
When you use XPath (XML Path Language) in Selenium, this is just a way to navigate through hierarchical structure of an XML-like document, such as html.
XPath uses a non-XML syntax to provide a flexible way of pointing to different parts of an XML document.
Internally selenium uses W3 XPathEvaluator, which evaluates XPath expressions.
You can study XPathEvaluator source code here.
Search Context
The SearchContext is a topmost interface present in the Selenium WebDriver hierarchy. It has two methods that will be the abstract as SearchContext is an interface.
findElement(): Find the first WebElement using the given method.
WebElement findElement​(By by)
Parameters:
by - The locating mechanism
Returns:
The first matching element on the current context
Throws:
NoSuchElementException - If no matching elements are found
findElements(): Find all elements within the current context using the given mechanism.
java.util.List<WebElement> findElements​(By by)
Parameters:
by - The locating mechanism to use
Returns:
A list of all WebElements, or an empty list if nothing matches
The browser DOM exposes API like querySelector,querySelectorAll, getElementById, getElementsByClassName, getElementsByName, etc through javascript that can be used to locate elements.
For example :
Navigate to www.bing.com
Press F12 to open developer console.
Enter document.querySelector("#sb_form_q") to locate search box input by css selector. I am using #Id here as css selector.
Enter document.getElementById("sb_form_q") to locate search box input by it's Id
Enter document.getElementsByClassName("sb_form_q")[0] to locate search box input by it's class name
Enter document.getElementsByName("q")[0] to locate search box input by it's name
All of above should return "<input id="sb_form_q" class="sb_form_q" name="q" type="search" maxlength="1000" autocomplete="off" aria-label="Enter your search term" autofocus="" aria-controls="sw_as" aria-autocomplete="both" aria-owns="sw_as" aria-activedescendant="sa_5004">" same result.
Selenium uses these DOM API to retrieve the elements. However, selenium might use these DOM API via some other mechanism (e.g C ++) and not by executing javascript for faster execution. XPath lookup is something not supported directly by browser DOM API. Selenium probably provides it's own implementation for XPath lookup or rely on some browser polyfill for this functionality.

How to cross check element is accessible from selected element locator on browser. Without including it into script and running in selenium?

For e.g. I have written a xpath for an element then how to check it is a valid xpath for that element without including it and executing the script in selenium. Same question is with using other element locators like id, css selectors.
Try Chrome Dev tool > Inspector > Console
for xpath you can use $x("valid xpath syntax")
for css selector you can use $$(".cssclass1.cssclass2")

Finding XPath on Selenium

please help me.
Actually I want to find element locator in selenium using XPath since the id is auto-generate (the ID always changed when the page refresh). but the XPath always changed too. here is the XPath locator :
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[15]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[7]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
Actually I already try to use :
html/body/div[class='scrollingMenu']/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
the div itself has unique classname scrollingMenu. but it is not working. it always give the error element not found.
You can use element's id in your XPath as follow:
Suppose id="constantPart-generatedPart12345", then XPath is
//*[contains(#id, "constantPart-")]
PS. If this not works, update your question with HTML for target element, so I can edit XPath appropriatelly
Open the page in Google Chrome and use F12 to test your xPath before implementing it - has saved me a lot of time

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.