How to handle dynamic Id in selenium? - xpath

I am working on automation and getting dynamic id each and every-time .Please find the sample code..
<li class="list-group-items pingMessage clearfix" id="59580" data-reactid=".c.$0.0.$59580"></li>
<li class="list-group-items pingMessage clearfix" id="59581" data-reactid=".c.$0.0.$59580"><li>
<li class="list-group-items myMessage pingMessage clearfix bunch" id="59588" data-reactid=".e.$0.0.$59578"></li>
Every <li> item has a different id. I want to get an item's id value using gettext then store it in a variable and call in xpath.
I tried following code :
//Getting id of particular text
WebElement Id=driver.findElement("By.xpath(//*[#class='list-group-items myMessage pingMessage clearfix bunch']").getText();
//My problem starts here i want to pass the stored id as id value how can i do it..
driver.findElement("By.id("+Id+")).click;

getText() is used to get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace while you need here to get element id attribute, So you should try using getAttribute() as below :-
//Storing the value of ID
WebElement Id = driver.findElement(By.cssSelector(".list-group-items.myMessage.pingMessage.clearfix.bunch")).getAttribute("id");

Please try with below xpath
//tagName[contains(#id='595')]

Related

fetching all the data from a partially hidden list watir

I'm trying to fetch a span of 4,600 elements
<span> 4,600 </span>
i inspected the elements and found that each element is a list class which has a child class with a title and href that i want to fetch the problem is that :
not all elements are visible , you would have to scroll down the api to find more elements
i cant seem to successfully fetch a single piece of data
puts browser.th(:class => %w("_9irns _pg23k _jpwof _gvoze")).link.hreflang
this is the structure of the code i'm trying to fetch
<ul class = 'xxx'>
<div class = 'xxa'>
<li class ='fff'>
<li class ='fff'>
<li class ='fff'>
.
.
the <li class = 'fff'> has <a class='xxx xxx xxx xxx'> having the data i'm trying to fetch tittle and href
to be more clear how can I iterate over all the classes of 'fff' and pick a url which is in a child class of it.
Don't use quotes inside the %w to find an element from a collection of classes, and try requiring watigiri gem and using #text! to obtain text of hidden elements.

How to refer Element using xpath inside multiple <li> that in turn contains <a> tag

How can i refer multiple elements present under li tag using xpath?
<div id="accordian">
<ul>
<li>
<h3 class="classroom"></h3>
<ul style="display: block;">
<li>name1</li>
<li>name2</li>
<li>name3</li>
<li>name4</li>
</ul>
</li>
i am using Selenium Webdriver, I tried following code to refer the element, but it returns a blank value.
List<WebElement> listelement=driver.findElements(By.xpath("//div[#id='accordian']/ul/li/ul/li"));
for(WebElement list: listelement)
{
System.out.println(list.getText());
}
List<WebElement> list=driver.findElements(By.xpath("//div[#id='accordian']/ul/li/ul/li"));
just add a tag at end of your xpath, that all this will work
//div[#id='accordian']/ul/li/ul/li/a"
*** This is a comment as I don't have access to Comments section ****
Hi,
Limit xpath till /ul and don't use /li. It will return list and then iterate over the child elements.
xpath("//div[#id='accordian']/ul/li/ul")
I doubt about the Xpath you tried, But below is the way you can achieve it.
List<WebElement> list=driver.findElements(By.xpath("//div[#id='accordian']/ul/li/ul/li"));
System.out.println("No of names present="+ list.size());
// use of for loop for iteration
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i).getText());
}
System.out.println("-------------------------");
//use of for each for iteration
for(WebElement wb: list)
System.out.println(wb.getText());
Do getText() on a tag elements. I always prefer using css over xpath. So here is my solution,
By byCss = By.cssSelector("#accordian>ul>li>ul>li>a");
List<WebElement> listElement = driver.findElements(byCss);
for(WebElement list: listElement)
{
System.out.println(list.getText());
}
I got the same problem and struggled with it for few days. You can use href tag as well to fetch the elements. Also You can try using 'a' tag. It will be something like this:
List<WebElement> listelement=driver.findElements(By.xpath("//div[#id='accordian']/ul/li/ul/li/a"));
for(WebElement list: listelement) {
String name= list.getAttribute("href");
System.out.println(name);
}
---should be comment, but don't have enough reputation---
I tried your solution on given HTML,
for me it is working fine for chromedriver and firefox.(printing all four values from list)
for InternetExplorer driver i am not able to get values, but it it because listElement.size() is 0
You can try
element.getAttribute("value") or elem.getAttribute("innerHTML");
to check what is happening here.
swapnil, your code with xpath is working for me, I get all the 4 elements, still you can try this as well
List<WebElement> listElements = driver.findElements(By.tagName("a"));
for(WebElement a : listElements){
System.out.println(a.getText());
}

Select href with id and class using xpath

let say I have DOM like this:
<div id="tabsmenu">
<ul>
<li class="one">foo</li>
<li class="two">baz </li>
</ul>
</div>
and I would like to get the text from <a href> elements:
# desired output: ['#foo', '#baz']
How to do it using xpath and using combination id and element with a specific class within id ?
Already tried:
some_doc.xpath('//a[#id="tabsmenu"]/[#class="ui-tabs-anchor"]/#href')
# select all href tags of any a element that is in id tabsmenu and class attribute ui- tabs-anchor
EDIT - corrected tabmenu into tabsmenu
You're most likely looking for something like this:
//div[#id='tabsmenu']//a[#class='ui-tabs-anchor']/#href
That will get all href attributes that are part of an a tag with the class ui-tabs-anchor and inside a div element with the id tabsmenu.
Also you might want to take a look at this question:
Find out if class name contains certain text
This is because the class will match the exact value (ui-tabs-anchor) and maybe some additional class might be added there such as class="ui-tabs-anchor disabled" and then there will not be a match in there.

How can I insert a space for an Action name that has more than one word in it?

I am trying to do this to display a breadcrumb on a page:
#if (ViewContext.RouteData.Values["controller"] != "MainController")
{
<li>
#Html.ActionLink(ViewContext.RouteData.Values["controller"]
.ToString(), "Index",
ViewContext.RouteData.Values["controller"].ToString())
</li>
}
#if (ViewContext.RouteData.Values["action"] != "Index")
{
<li>
#Html.ActionLink(ViewContext.RouteData.Values["action"]
.ToString(), ViewContext.RouteData.Values["action"].ToString(),
ViewContext.RouteData.Values["controller"].ToString())
</li>
}
This works well to display "Controller" or "Controller > Action" (without the ">" - am using jBreadcrumbs to display a divider).
However, if my Action names are like ScheduleAppointment, for example, then it's displayed as "Controller > ScheduleAppointment" whereas I'd like it displayed as "Schedule Appointment" (i.e., a space is inserted).
Is it possible to do that in a #{} function on the page? Or do I have to write an extension?
In either case a code example would help greatly. Thanks.
You can create your custom attribute with "DisplayName" property and put it on your action. Then in view you can access current controller
ViewContext.Context
and get current action's custom attribute instance using reflection and get "DisplayName" property value.
Or you can set property in ViewBag and then use it in view.

Using Xpath to get text from span- webdriver

I'm using selenium webdriver to get some text on my webpage using xpath.
This is the code
<a class="ng-binding" data-toggle="tab" href="#tabCreatedByMe">
Created By Me
<span class="badge ng-binding">3</span>
</a>
I need to get the number '3'. this number is changing everytime
I made this code but it does not return anything
public String getAmountSubtab1() throws InterruptedException{
WebElement s = driver.findElement(By.xpath("//*[#class='badge ng-binding']"));
return s.getText(); }
Suggestions?
Are you sure you have only one span with the class badge ng-binding It might be that you might have another span before this with the same class name. Advised not to use class name when identifying an element. Use this xpath. Should work.
//a[contains(text(), 'Created By Me')]/span

Resources