I need to select the last item in a DYNAMIC list. The following is my script. Thanks!
WebElement selectElement = driver.findElement(By.name("siteKey"));
Select select = new Select(selectElement);
//select.selectByVisibleText("last item");
//select.selectByIndex(0);
//select.selectByValue("value");
Please see the page HTML below. Let me know if I can provide you with any other info. Thanks!
<div id="overview_form">
<ol>
<li>
<span>1.</span>
<label class="input_label" for="sites">Sites*</label>
<div class="select2-container select2-container-active select2-dropdown- open" id="s2id_autogen1" style="width: 500px;">
<a tabindex="-1" class="select2-choice" onclick="return false;" href="javascript:void(0)">
<span>www.roger.com_20150210075155</span>
<abbr style="display:none;" class="select2-search-choice-close"></abbr>
<div><b></b></div></a>
<input type="text" class="select2-focusser select2-offscreen" disabled="disabled">
</div>
<select style=" size="1" name="siteKey" class="select2-offscreen" tabindex="-1">
<option value="30518">www.roger.com_20150209191817</option>
<option value="30520">www.roger.com_20150209192123</option>
<option value="30522">www.roger.com_20150209192351</option>
<option value="30524">www.roger.com_20150209192910</option>
<option value="30528">www.roger.com_20150209193425</option>
<option value="30529">www.roger.com_20150209193801</option>
<option value="30531">www.roger.com_20150209194009</option>
<option value="30546">www.roger.com_20150210074133</option>
<option value="30548">www.roger.com_20150210074359</option>
<option value="30550">www.roger.com_20150210075155</option></select>
</li>
</ol>
</div>
How about something like:
WebElement selectElement = driver.findElement(By.name("siteKey"));
Select select = new Select(selectElement);
select.selectByIndex(select.getOptions().size()-1);
You can use getOptions(), it will return a list then get the size of the list
Select select = new Select(driver.findElement(By.id("siteKey")));
List<WebElement> l = select.getOptions();
int numElements = l.size();
select.selectByIndex(munElements);
Hope this helps
I tried below way, and it's working.. Maybe this will help u .
WebDriver driver = new FirefoxDriver();
driver.get("http://www.echoecho.com/htmlforms11.htm");
Thread.sleep(100l);
driver.findElement(By.xpath("//select[#name='dropdownmenu']")).click();
Thread.sleep(100l);
WebElement element = driver.findElement(By.xpath("//select[#name='dropdownmenu']"));
List<WebElement> elements = element.findElements(By.tagName("option"));
System.out.println(elements.get(elements.size() - 1).getText());
Related
<div class="pull-right">
<button type="button" class="btn-excel-select-prd btn btn-sm btn-base">엑셀다운로드</button>
<select id="selectLength" cdgrp="PAGE_LEN" class="form-control input-sm" style="">
<option value="10" title="10개씩 보기">10개씩 보기</option>
<option value="20" title="20개씩 보기">20개씩 보기</option>
<option value="50" title="50개씩 보기" selected="">50개씩 보기</option>
<option value="100" title="100개씩 보기">100개씩 보기</option>
</select>
</div>
enter image description here
I want to choose the 4th option.
However, if I write the code as below, an error "element not interactable: Element is not currently visible and may not be manipulated" appears. How can I solve it?
WebElement selectElement = driver.findElement(By.id("selectLength"));
Select select = new Select(selectElement);
select.selectByIndex(3);
enter image description here
There is a dropdown with 9 options in it, all of anchor (a) type. Selecting an option from the list, changes its class & other attributes dynamically.
This is how the html code looks like:
<select data_old_value="Email" id="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843" name="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843" onchange="RuleWizardIns.RuleActionChanged($(this))" style="width: 103px !important; height: auto !important; display: none;" aria-disabled="false"><option value="Email" selected="selected">Send Email</option><option value="TaskCreate">Create Task</option><option value="AddActivity">Add Activity</option><option value="AddToList">Add To List</option><option value="LeadUpdate">Update Lead Fields</option><option value="TagLead">Tag Lead</option><option value="StarLead">Star Lead</option><option value="UnStarLead">Unstar Lead</option><option value="Webhook">Call Webhook</option></select>
<option value="Email" selected="selected">Send Email</option>
<option value="TaskCreate">Create Task</option>
<option value="AddActivity">Add Activity</option>
<option value="AddToList">Add To List</option>
<option value="LeadUpdate">Update Lead Fields</option>
<option value="TagLead">Tag Lead</option>
<option value="StarLead">Star Lead</option>
<option value="UnStarLead">Unstar Lead</option>
\<option value="Webhook">Call Webhook</option>
</select>
<span>
<a class="ui-selectmenu ui-widget ui-state-default ui-corner-all ui-selectmenu-dropdown" id="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843-button" role="button" href="#nogo" tabindex="0" aria-haspopup="true" aria-owns="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843-menu" style="width: 103px;" aria-disabled="false"><span class="ui-selectmenu-status">Send Email</span><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span></a>
<span class="ui-selectmenu-status">Send Email</span>
<span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span>
</a>
</span>
I tried to get the control on Update Lead Fields option by using below code, but unfortunately its not working.
Select ActionType = new Select(driver.findElement(By.xpath("//select[#data_old_value='Email']/following-sibling::span/a")));
ActionType.selectByVisibleText("Update Lead Fields");
I'm getting below error:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a"
And there is no ui for select attribute as you can see from below image:
https://d24cdstip7q8pz.cloudfront.net/t/t20170511180558/content/common/images/Untitled.png
A Select object can only be created by getting the WebElement with tag is "select" that's what the errormessage is about. I created an C# example which I tested giving expected result.
Driver.Url = ("https://developer.mozilla.org/en-
US/docs/Web/HTML/Element/select");
var valuesSelectElement = Driver.FindElement(By.Name("select"));
var selectElement = new SelectElement(valuesSelectElement);
// To select an option by value
selectElement.SelectByValue("value1");
// To select an option by text
selectElement.SelectByText("Value 2");
var options = selectElement.Options;
//To click on one of the options
foreach (var option in options)
{
if (option.Text.Equals("Value 3"))
{
option.Click();
}
}
I am unable to select an option dynamically using select commands by value, visible text or index, the workaround was i can use actions to click select and select a particular option or sending keys ArrowDown which is not a best way to implement because i want to use the same select function globally to automate UI all over the website like a keyword function.
sample script below:
WebElement selectElement = driver.findElement(By.xpath(.//*[#id='XYZPopUpForm:j_idt90:country_label']));
Actions selectItem = new Actions(driver);
selectItem.moveToElement(selectElement).click().perform();
selectItem.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).perform();
selectItem.sendKeys(Keys.ENTER).perform();
HTML Code :
<tr>
<td>
<label for="XYZPopUpForm:j_idt90:country">Country</label>
</td>
<td>
<div id="XYZPopUpForm:j_idt90:country" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all">
<div class="ui-helper-hidden-accessible">
<div class="ui-helper-hidden-accessible">
<select id="XYZPopUpForm:j_idt90:country_input" tabindex="-1" name="XYZPopUpForm:j_idt90:country_input">
<option selected="selected" disabled="disabled">--select--</option>
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<option value="AI">Anguilla</option>
<option value="AQ">Antarctica</option>
<option value="AG">Antigua and Barbuda</option>
<option value="AR">Argentina</option>
<option value="AM">Armenia</option>
<option value="AW">Aruba</option>
<option value="AU">Australia</option>
<option value="AT">Austria</option>
<option value="AZ">Azerbaijan</option>
<option value="BS">Bahamas</option>
<option value="BH">Bahrain</option>
<option value="BD">Bangladesh</option>
<option value="BB">Barbados</option>
<option value="BY">Belarus</option>
<option value="BE">Belgium</option>
<option value="BZ">Belize</option>
<option value="BJ">Benin</option>
<option value="BM">Bermuda</option>
<option value="BT">Bhutan</option>
<option value="BO">Bolivia, Plurinational State of</option>
<option value="BQ">Bonaire, Sint Eustatius and Saba</option>
<option value="BA">Bosnia and Herzegovina</option>
<option value="BW">Botswana</option>
<option value="BV">Bouvet Island</option>
<option value="BR">Brazil</option>
<option value="IO">British Indian Ocean Territory</option>
<option value="BN">Brunei Darussalam</option>
<option value="BG">Bulgaria</option>
<option value="BF">Burkina Faso</option>
<option value="BI">Burundi</option>
<option value="KH">Cambodia</option>
<option value="CM">Cameroon</option>
<option value="CA">Canada</option>
<option value="CV">Cape Verde</option>
</select>
</div>
Updated Script:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } } ", selectElement, "Afghanistan");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("XYZPopUpForm:j_idt90:country_input")));
Screenshot of HTML to be more clear :
Seems you are finding incorrect element By.xpath your drop-down id looks like XYZPopUpForm:j_idt90:country_input but you are finding id XYZPopUpForm:j_idt90:country_label in By.xpath, May be that is the problem. Try using Select with correct drop-down id as below :-
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
//Now use select
Select select = new Select(selectElement);
select.selectByVisibleText("Afghanistan");
or
sel.selectByIndex(1);
or
sel.selectByValue("AF");
Edited :- If above does not work due to visibility of element you should try using JavascriptExecutor as below :-
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", selectElement, "Afghanistan");
Hope it works...:)
I need to select the last item in the list. My code below shows the message that the element is not currently visible. How can fix this?
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#s2id_autogen1 > a.select2-choice > span")));
WebElement element = driver.findElement(By.xpath("//select[#name='siteKey']"));
element.click();
Select select = new Select(element);
select.selectByIndex(select.getOptions().size()-1);
HTML:
<div id="overview_form">
<ol>
<li>
<span>1.</span>
<label class="input_label" for="sites">Sites*</label>
<div class="select2-container select2-dropdown-open select2-container-active" id="s2id_autogen1" style="width: 500px;">
<a tabindex="-1" class="select2-choice" onclick="return false;" href="javascript:void(0)">
<span>gn</span><abbr style="display:none;" class="select2-search-choice-close"></abbr><div><b></b></div></a>
<input type="text" class="select2-focusser select2-offscreen" disabled="disabled"></div>
<select style="width:500px; *width:400px;" size="1" name="siteKey" class="select2-offscreen" tabindex="-1">
<option value="30706">gn</option>
<option value="30813">www.walmart.com_20150212151258</option>
<option value="30815">www.walmart.com_20150212151452</option>
<option value="30817">www.walmart.com_20150212152338</option>
<option value="30819">www.walmart.com_20150212152521</option>
<option value="30820">www.walmart.com_20150212152849</option>
<option value="30822">www.walmart.com_20150212152939</option>
<option value="30824">www.walmart.com_20150212153438</option>
<option value="30761">www.yup.com</option>
</select>
</li>
</ol>
Screenshot of the List. This list has a search field, where user can enter prefixes to narrow-down the search.
Try this. Please make sure to expand the list as needed. The selector is written such a way so that it will always fine the last option of the select tag named siteKey
//this selector find the last child o
By cssSelector = By.cssSelector("[name='siteKey']>option:last-child");
//explicit wait to make sure the element present
WebElement element = new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(cssSelector));
element.click();
System.out.println(element.getText());
Print
www.yup.com
hii all i am very new to automation
my problem is i am getting an error
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "td"
when i am trying to select an option from dropdown list
this is my sourch code
public void CompanyFilters_Fn() {
// TODO Auto-generated method stub
_driver.findElement(By.xpath(".//*[#id='company._idCell']")).click();
Select CompanyOptions = new Select(_driver.findElement(By.xpath(".//*[#id='company._idCell']")));
CompanyOptions.deselectAll();
CompanyOptions.selectByIndex(5150);
CompanyOptions.selectByIndex(5003);
}
this is my html code
<td id="company._idCell" class="filterValue filterActive filterChanged firepath-matching-node" onclick="showInputElement('pre.myProjectsForm.company._id', this, null, 'auto'); return false;" title="Select to filter by Company">Company: [No Company]</td>
these are the options
<div id="divSelect" class="inputElement" style="left: -1000px; top: 212px; width: auto; height: 20px;">
<select id="inputSelect" size="5" style="width: auto; visibility: visible;" multiple="" disabled="">
<option value="-1">Any</option>
<option value="0">[No Company]</option>
<option value="5150">123</option>
<option value="5203">AutoCarLoanCompany</option>
<option value="5205">Company_rahul</option>
<option value="5202">CreditCardCompany</option>
<option value="5003">IBM</option>
<option value="5204">June</option>
<option value="5300">other companyyyyy</option>
<option value="5001">qa</option>
<option value="5101">Rahul</option>
</select>
</div>
Plzzz help me to get out of this...
The error message is very descriptive, you've given it a td when it expects a select (rightly so).
So here:
Select CompanyOptions = new Select(_driver.findElement(By.xpath(".//*[#id='company._idCell']")));
You need to change this to give it the select:
Select CompanyOptions = new Select(_driver.findElement(By.id("inputSelect")));