I am trying to locate an xml element in selenium using xpath. It is nested in inside .
I have numbers 3498546 and 3498755 in hand. I need to match these numbers to the numbers listed in elements and locate the specific .
I have tried using various combinations with below:
xpath=//*[#id="tabs"]/ul/li/[contains(div,'3498546')]
But it never worked.
Below is and example html code, I have around 100 listed in
<div id="tabs">
<ul>
<li class="unknown">
<span style="">DELIVERED</span>
<a title="A1" onclick="submitForm('e1:eForm',1);return false;"
class="eLink" href="#">
<div class="c1">"Year 2008
"
<br>"3498546
"
<br>
</div>
<strong>Date: </strong>05/14/2019
</a>
</li>
<li class="unknown">
<span style="">DELIVERED</span>
<a title="A2" onclick="submitForm('e1:eForm',1);return false;"
class="eLink" href="#">
<div class="c1">"Year 2008
"
<br>"3498755
"
<br>
</div>
<strong>Date: </strong>05/14/2019</a>
</li>
</ul>
</div>
I want to be able to locate and click the element which has the text 3498546 or 3498755.
Here is the xpath based on the 3498546.
//div[#class='c1'][contains(normalize-space(.),'3498546')]
you can change the 3498546 value to the required number and use the same. In case you want to get all the elements having numbers and click each one of them then you can use the below xpath.
//div[#id='tabs']//div[#class='c1']
To click() on either of the elements with text as 3498546 or 3498755 you can use the following solutions:
3498546:
Java & partialLinkText:
driver.findElement(By.partialLinkText("3498546")).click();
3498755:
Java & partialLinkText:
driver.findElement(By.partialLinkText("3498755]")).click();
//div[contains(text(),'3498755')]
All you need is to look for the div element that its text contains the numbers you're looking for.
Related
I'm trying to create a selector that matches "several" items in my dom.
Dom may look like this:
<a class="email-item">
<span class="title">Newsletter</span>
<span class="from">a#a.com</span>
</a>
<a class="email-item">
<span class="title">Newsletter</span>
<span class="from">a#a.com</span>
</a>
<a class="email-item">
<span class="title">Newsletter</span>
<span class="from">b#b.com</span>
</a>
I try to find all a-elements that do have the title "Newsletter" as innerText AND are from "a#a.com"
so kind of a combo-selector...
Thanks for your help!
Ray
Use the filter method to find elements that match the conditions.
Please help me the locate the check box and select. There are multiple check boxes, there is no way to locate them uniquely.
Here is the Code for one of such check box.
Thanks in Advance for the Help!!!
<div class="col-xs-2 col-sm-2 col-lg-2" style="height:65px">
<ul class="list-inline pull-right">
<li>
<md-input-container class="md-block">
<md-checkbox value="$index+1check" class="checkbox ng-valid ng-dirty ng-touched ng-empty" ng-model="item.Selectedd" ng-click="toggle($index+1, selected,item.TitleId,item)" icon,md-checkbox.md-checked._md-icon="{background-color: green;}" id="Cbk_List" role="checkbox" tabindex="0" aria-checked="false" aria-invalid="false" style=""><div class="_md-container md-ink-ripple" md-ink-ripple="" md-ink-ripple-checkbox=""><div class="_md-icon"></div></div><div ng-transclude="" class="_md-label">
</div></md-checkbox>
</md-input-container>
</li>
<li>
<div class="manageTitle_CirclCard">
<div class="ng-binding">2</div>
</div>
</li>
</ul>
</div>
You should try to locate using cssSelector with it's attribute tabindex as below :-
#Cbk_List[tabindex='0']
Edited :- If checkbox element has not any attributes with unique value, you should try using xpath with index, assuming you want to get first checkbox element, then try below xpath :-
(.//*[#id = 'Cbk_List'])[1]
Note :- In above xpath, just change the index from 1 to desire one checkbox to find.
application consist dynamic search box &
while typing "Mumbai" search results drop down display as below,
But through selenium as,
driver.findElement(By.id("searchstr2")).sendKeys("Mumbai");
or
driver.findElement(By.id("searchstr2")).sendKeys("Mumbai"+ARROW_DOWN);
search result not display as,
text box html as,
<input id="searchstr2" class="search ui-autocomplete-input" type="text" placeholder="Search for Building, Location or Developer" autocomplete="off" name="searchstr2" size="35" style="background-image: none;"
after successful search list display as,
<li id="ui-id-117" class="ui-menu-item" tabindex="-1">
<a>
Nariman Point -
<b style="font-size:11px">
<span style="font-size:.8em; /*color:#EE7600;*/ color:#888888; float:right;">locality</span>
</a>
</li>
<li id="ui-id-118" class="ui-menu-item" tabindex="-1">
<li id="ui-id-119" class="ui-menu-item" tabindex="-1">
<li id="ui-id-120" class="ui-menu-item" tabindex="-1">
<li id="ui-id-121" class="ui-menu-item" tabindex="-1">
please suggest.
The problem with the autocomplete fields is that usually there is a Javascript event that waits for text to be present in the field in order to display the available suggestions (could be Ajax that gets executed to get the suggestions from the server). SendKeys doesn't trigger that event, so you could try to click into the field after you've entered you're text.
Basically:
WebElement suggestion = driver.findElement(By.id("searchstr2"));
suggestion.sendKeys("Mumbai");
suggestion.click();
I didn't test this, so you could try using the click() method before sendKeys().
If this doesn't fix your issue, you could try to do it by using Javascript to trigger your onChange event:
WebElement suggestion = driver.findElement(By.id("searchstr2"));
suggestion.click();
suggestion.sendKeys("Mumbai");
((JavascriptExecutor) driver).executeScript("$(arguments[0]).change(); return true;", suggestion);
java scripts takes some time to load the list so i have added timer in between send name & arrow down key event will be works for me code snap as,
WebElement ar=driver.findElement(By.id("searchstr2"));
ar.sendKeys("Mumbai");
Thread.sleep(2000);
ar.sendKeys(Keys.ARROW_DOWN);
I have a complected structure of the page and I have no idea how to find the element...
The page contains folders that are created by a user, I need to create a folder and then to click on it, but I have no idea how to find the element that I've created. The structure look like this:
<div class="row-text" style="width: calc(100% - 84px);">
<span class="row-item-name">
<span class="row-item-link">
<a class="grid-row-element-name">Eclipse111</a>
</span>
<span class="row-item-actions hover-child">
<a>Share</a><span> | </span><a watchdox-rename="name" watchdox-save-func="rename(element, name)" class="rename-link"><span translate="">Rename</span></a>
</span>
</span>
<br>
<span class="row-meta-data">
<span class="creation-date-formatted">Today at 10:30 | </span>
<span class="row-email">orgadmin#mailinator.com</span>
</span>
</div>
<div class="grid-row-buttons">
<div class="row-tools">
<div class="btn-group dropdown" uib-dropdown="">
<button type="button" class="btn btn-default uib-dropdown-toggle clear-button dropdown-toggle" uib-dropdown-toggle="" aria-haspopup="true" aria-expanded="false">
<span class="icon-wd-material-menu"></span>
</button>
<ul uib-dropdown-menu="" class="dropdown-menu-highZ contextual-menu dropdown-menu" role="menu">
</ul>
</div>
</div>
</div>
The class="grid-row-element-name" contains the name of the folder that was created (each folder has its own )....
I have no idea how to continue with the testing cause I am not able to click on the folder....
Thank you.
try the following, since you said "grid-row-element-name" has the folder name then trying using that class name in cssSelector.
List<WebElement> elements = driver.findElements(By.cssSelector("a.grid-row-element-name"));
String folderName = null; //name of the folder which you want to click.
for (WebElement ele : elements) { //Iterate over the loop
if (ele.getText().equalsIgnoreCase(folderName)) {
ele.click();//once the folder you want is found go for the click.
}
}
//OR
//To click on the last element try this
elements.get(elements.size()-1).click();
Since you are looking for an element with specific text that you have created (the folder name), I would approach this by looking for an A tag that contains the folder name.
BTW, you didn't tag your question with a language so the below is Java. You should be able to convert it easily to another language by just reusing the XPath, if needed.
String folderName = "Eclipse111";
// create folder
WebElement folder = driver.findElement(By.xpath("//a[text()='" + folderName + "']"));
I want to use the WebElement.findElement() API to locate a node inside the parent node using XPATH //span[#class='child-class']. I thought this would return me the <div> that is inside the parent. However, it is returning me the first one it found in the entire DOM tree. Did I use the wrong XPATH?
I have also tried using .//span[#class='child-class'] as the XPATH, but that does return anything.
Thank you.
UPDATE:
given the HTML below, I want to define a locator for the child-title <span> and child-date <span> and locate them using WebElement.findElement() API regardless of the parent being "//a/li[1]" or "//a/li[2]"
<a>
<li> parent 1
<div>
<span class="child-title child-style">title 1</span>
<span class="child-date child-style"> date 1</span>
<span class="child-author">author 1</span>
</div>
</li>
</a>
<a>
<li> parent 2
<div>
<span class="child-title child-style">title 2</span>
<span class="child-date child-style"> date 2</span>
<span class="child-author">author 3</span>
</div>
</li>
</a>
<a>
<li> parent 3
<div>
<span class="child-title child-style">title 3</span>
<span class="child-date child-style"> date 3</span>
<span class="child-author">author 3</span>
</div>
</li>
</a>
I have a WebElement parent2 initialized and located using "//a/li[2]",
WebElement child = parent2.findElement(By.xpath("//span[#class='child-author']")); would give me "author 1"
WebElement child = parent2.findElement(By.xpath("span[#class='child-author']")); would give me NoSuchElementException
There are my 2 comments with your sample code
1 - With your posted HTML, the xpath //a/li[2] is not found (we only have 3 elements with //a/li[1])
2 - Assume that we do have right code, you need to understand the differences between single slash and double slash in Xpath
a/b (single slash): select element that has "tag b" and "stands right after" an element that has "a tag"
E.g.:
<a>
<b>
<d>
<c>
</c>
</d>
</b>
</a>
AND
a//b (double slash): select element that has "tag b" and is n-level-child an element that has "a tag"
E.g.:
<a>
<c>
<d>
<b>
</b>
</d>
</c>
</a>
So, with your code
<a>
<li> parent 1
<div>
<span class="child-title child-style">title 1</span>
<span class="child-date child-style"> date 1</span>
<span class="child-author">author 1</span>
</div>
</li>
</a>
If you want to get Date Info, you should use
WebElement parent = driver.findElement(By.xpath("//a/li"));
WebElement date = parent.findElement(By.xpath("div/span[contains(#class, 'child-date')]"));
WebElement date = parent.findElement(By.xpath("//span[contains(#class, 'child-date')]"));
The code
WebElement date = parent.findElement(By.xpath("span[contains(#class, 'child-date')]"));
Will bring out NoSuchElementException because there is no [span] tag right after [li] tag
Hope help
Try something like:
Use dot(.) before double slash(//)
It looks for child under the given parent element.
Completely new question ... completely new answer. :(
Try something like:
WebElement parent1 = driver.findElement(By.xpath("//a[1]/li")); // use a[2] for parent2
WebElement author = parent1.findElement(By.xpath("span[#class='child-author']"));
WebElement date = parent1.findElement(By.xpath("span[contains(#class, 'child-date')]"));
WebElement title = parent1.findElement(By.xpath("span[contains(#class, 'child-title')]"));
Try something like :
//a/li[contains(text(), 'parent 1')]/div
It requests for "the <div> inside a <li> whose text contains 'parent 1' and who is inside a <a>.
It might not work if you have more parents because it works with a contains() (this xpath would also select <li> parent 10 ... </li>). It would be better if "parent x" were an attribute of the <li> instead of its text.