Selection issues with the hidden tag - selenium

I am unable to access the select option in this.
<tooltip-component params="id:'title1',title:'Alert name should be unique',isImportant:true"></tooltip-c
<br>
<select class="chosen-select" data-placeholder="Alert Type" id="alert_type" data-bind="options:alertType,optionsText: 'name', optionsValue: 'id',chosenSelectedOptions: selected Alert,valueAllowUnset: true" ></select>
How can I make this drop list visible and accessible??

You can use ID to locate it , try this code
Select dropdown= new Select(driver.findElement(By.id("alert_type")));
dropdown.selectByVisibleText("Value under Dropdown");.
You can also use xpath
Select dropdown= new Select(driver.findElement(By.xpath("//*[#id='alert_type']")));
dropdown.selectByVisibleText("Value under Dropdown");

Try any of the below mentioned answers.
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("alert_type")));
new Select(driver.findElement(By.id("alert_type"))).selectByVisibleText("Text Name Under Your Dropdown");
OR
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("alert_type")));
new Select(driver.findElement(By.id("alert_type"))).selectByIndex(0); //Indexing start from zero
OR
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("alert_type")));
new Select(driver.findElement(By.id("alert_type"))).selectByValue("Value Name Under your Dropdown");

Related

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();
}

How to select first 5 webelements and iterate?

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]));

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);

vb.net get all attributes value using htmlagilitypack

this is the html
<div id="catlist-listview" class="cat-listview cat-listbsize">
<ul>
<li>title1</li>
<li>title2</li>
<li>title3</li>
<li>title4</li>
<li>title5</li>
<li>title6</li>
<li>title7</li>
<li>title8</li>
<li>title9</li>
<li>title10</li>
</ul>
</div>
and my code is
dim htmldoc as new htmldocument
htmldoc.loadhtml(source)
for each link as htmlnode in htmldoc.document.selectnodes("//*[#id='catlist-listview']/ul")
textbox3.text = link.innerhtml
next
the output is
<li>title1</li>
<li>title2</li>
<li>title3</li>
<li>title4</li>
<li>title5</li>
<li>title6</li>
<li>title7</li>
<li>title8</li>
<li>title9</li>
<li>title10</li>
i want get all and only http://wantedlink1 to http://wantedlink10
i try attributes("href") but i get only one link
i want to list all the link like this :
http://wantedlink1
http://wantedlink2
http://wantedlink3
.
.
.
http://wantedlink10
any help ??
Basically, you can change XPath for SelectNodes() to be selecting individual <a> elements instead of <ul>. Then from this point, it will be easy to iterate through the result and get href attribute one by one. Or you achieve the same using LINQ, like the following for example :
'select <a> elements'
Dim links = htmldoc.Document.SelectNodes("//*[#id='catlist-listview']/ul/li/a")
'project to IEnumerable of href attribute value'
Dim hrefs = links.Cast(Of HtmlNode)().Select(Function(x) x.GetAttributeValue("href", ""))
'join the `hrefs`, separated by newline, into one string'
textbox3.text = String.Join(Environment.NewLine, hrefs)
dotnetfiddle demo

Using Linq to XML foreach to create muliple XElements

I'm building an XML file dynamically using new XElement(), and midway through the file creation I need to iterate over a set of child records and create XElements for them. The problem I'm having is that I need to create more than 1 XElement per iteration. This is a my loop:
from t in trans.SalesTransactionLines
select new XElement("text", new XAttribute("lang", "en"), t.ItemName)
This works fine, but I need an additional 'position' XElement before each 'text' Element. This is the kind of thing I want, which doesn't work:
from t in trans.SalesTransactionLines
select new XElement("position",new XAttribute("x", "40"), new XAttribute("y", "420")),
new XElement("text", new XAttribute("lang", "en"), t.ItemName)
This is the result I'm looking for:
<position x="40" y="420" />
<text>Fender Classic Strat 70s Natural RN</text>
<position x="40" y="420" />
<text>Fender Classic 50s Tele White Blonde</text>
Use method based syntax and SelectMany method instead:
trans.SalesTransactionLines.SelectMany(x => new[] {
new XElement("position",
new XAttribute("x", "40"),
new XAttribute("y", 420")),
new XElement("text",
new XAttribute("lang", "en"),
x.ItemName)
})