How to select first 5 webelements and iterate? - selenium

List nines=driver.findElements(By.xpath(("//select[contains(#id,'ddlIN_HH')]")));
for(WebElement nine : nines)
{ Select s= new Select(nine);
s.selectByValue("09");
}

you can select first 5 elements directly using xpath function position()
so your new xpath would be
List nines=driver.findElements(By.xpath(("//select[contains(#id,'ddlIN_HH')]")[position() < 6]));

Related

Rendering a dynamic list of labels from a List<String>

I need to do something similar to:
#Query("MATCH (n:`:#{literal(#label)}`) WHERE n.entityId=$entityId RETURN n")
Mono<VisibilityGroup> findNode(UUID entityId, String label);
but specifying a list of labels in OR:
Mono<VisibilityGroup> findNode(UUID entityId, List<String> labels);
if label is {"A","B"} this should generate:
MATCH (n:A|B) WHERE...
What is the correct syntax to achieve this?

Searching for n-th child in n-th parent

There is a strange behaviour when it comes to finding elements by xpath. The situation:
<body>
...
<div class="ingredients-group">
<div class="group-header">
<h3>Title 1</h3>
... other stuff
</div>
</div>
<div class="ingredients-group">
<div class="group-header">
<h3>Title 2</h3>
... other stuff
</div>
</div>
...
I want to check the text of the H3 tag on the second ingredient-group. So I did the following in Selenium:
WebElement group2 = driver.findElement(By.xpath("//div[#class='ingredients-group'][2]"));
WebElement title2 = group2.findElement(By.xpath("//h3"));
String titleText = title2.GetText();
The last statement returns "Title 1". I would expect it to return "Title 2".
Strangely, this statement returns "Title 2":
String titleText = driver.findElement(By.xpath("//div[#class='ingredients-group'][2]//h3")).getText();
I would like to use the first option (group2.findElement), because there are several other elements in the containers I would like to refer to without having to write the full xpath.
Any ideas on this?
Thanks
Use findElements to return a list of webelements (h3 tags) and then access them as you would any other list:
WebElement groups = driver.findElements(By.xpath("//div[#class='ingredients-group'][2]"));
WebElement theH3Tag = groups[0].findElement(By.xpath(".//h3")); //make this a relative xpath
String titleText = theH3Tag.GetText();
WebElement the2ndH3Tag = groups[1].findElement(By.xpath(".//h3")); //make this a relative xpath
String titleText = the2ndH3Tag.GetText();
Or loop through the list:
WebElement[] groups = driver.findElements(By.xpath("//div[#class='ingredients-group'][2]"));
for (WebElement group : groups) {
WebElement h3Tag = group.findElement(By.xpath(".//h3")); //make this a relative xpath
String titleText = h3Tag.GetText();
}

Selecting value from oi-select dropdown in selenium

In my application there is oi-select dropdown which contains dynamic value. I want to select value from dropdown. I tried below code but it always select 1st element in the list
for(WebElement skill:selectSkill) {
System.out.println(".............................."+skill.getText());
if(skill.getText().equals(expectedSkills)) {
skill.sendKeys(Keys.ENTER);
break;
}
}
Instead of skill.sendKeys(Keys.ENTER); , use below code-
Select select = new Select(driver.findElement(By.xpath("xpath of the dropdown here")));
select.selectByVisibleText(expectedSkills);

How to use Index attribute of groupby clause using foreach loop in asp.net MVC?

I'm trying to run two foreach loops. The first will use group by clause to group all element, then, according to that grouping, I want to run another loop using index attribute of group by clause. How to achieve that?
Here's what I've got so far:
foreach (var amount in
Model.DebitCreditNoteEntries.GroupBy(x => x.AccountName.AccountHead))
{
for (int i = 0; i <= """use index attribute of groupby""" i++)
{
////index attribute contains total no of repeated fields.
}
}
If your inner loop is simply looping through each item in the group then the following should do it. Uses Count property.
foreach (var amount in Model.DebitCreditNoteEntries.GroupBy(x => x.AccountName.AccountHead))
{
for (int i = 0; i <= amount.Count i++)
{
////index attribute contains total no of repeated fields.
}
}
You could also do:
var grouping = Model.DebitCreditNoteEntries.GroupBy(x => x.AccountName.AccountHead);
foreach (var amount in grouping)
{
foreach(var creditNoteEntry in amount)
{
////index attribute contains total no of repeated fields.
}
}
if I understood your question correctly then what you want is the Key of the IGroupable.
the Key is the value that you grouped by.
if you had a vehicle class where a "type" property could be "car" or "truck"
and you used GroupBy("Type") then the Key of the group with all the cars woyld be "car"
and the Key of the group with all the trucks would be "Truck"
foreach (var amount in
Model.DebitCreditNoteEntries.GroupBy(x => x.AccountName.AccountHead))
{
for (int i = 0; i <= amount.Key i++)
{
////index attribute contains total no of repeated fields.
}
}
now if you need an actual index you could do something like this:
list.GroupBy(c => c.AccountName.AccountHead)
.Select((AccountName, ind) =>
new
{
Name = details.Key,
data= AccountName,
Index = ind
});
this would be an actual index of each group. singe Igropable does not implement an index you have to make your own.
I am on mobile sorry for the poor punctuation and grammar.

How to sort result by field?

I have a simple example:
result = db.command({
'findandmodify' : 'Task',
'remove' : True
})
print result
How to get first inserted element ? ( order by _id )
The find_one method in pymongo should return the first document in the collection: http://api.mongodb.org/python/current/tutorial.html#getting-a-single-document-with-find-one