Following is the code:
public static void test1() {
System.out.print("\nTo find UserName element");
Select select = new Select(driver.findElement(By.id("drop_down")));
System.out.print("\nElements found");
select.selectByIndex(1);
}
Neither following wroks:
select.selectByIndex(1);
select.selectByValue("1");
select.selectByVisibleText("Super Admin");
It throws an exception:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate option with value: 1
<select id="drop_down" style="width:205px;" name="drop_down">
<option value=""></option>
<option value="1">
Super Admin
</option>
<option value="4">
Question Reviewer
</option>
<option value="6">
Evaluator
</option>
</select>
May be the dropdown is not properly loaded, when you try to access it.
Try the below code to wait till the number of options in the dropdown becomes greater than 1, and then select the first option from it:
try{
// Waits for 20 seconds
WebDriverWait wait = new WebDriverWait(driver, 20);
// Wait until expected condition size of the dropdown increases and becomes more than 1
wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver)
{
Select select = new Select(driver.findElement(By.id("drop_down")));
return select.getOptions().size()>1;
}
});
//To select the first option
Select select = new Select(driver.findElement(By.id("drop_down")));
select.selectByVisibleText("Super Admin");
}catch(Throwable e){
System.out.println("Error found: "+e.getMessage());
}
Hi as per the first comment bu Subh on Jan 15 at 6:35 I too modified the code but getting the same error as mentioned by Abhinav on Jan 15 at 6:53 after that Subh said "I have edited my code above.. See if it works out for you and let me know too, please.." but I didn't see any modified code after this comment hence this didn't help....finally I searched few other forums and I tried using selectByIndex() as:-
WebElement toactTyp=driver1.findElement(By.name((<Name of the Element to access>)));
Select toactSel=new Select(toactTyp);
toactSel.selectByIndex(2);
It worked well with the above code.....I request to please share the modified code or atleast the lines where the modification has been done as it useful to a lot like me
Related
I'm trying to retrieve my select option from 3 databases located in a connection that's not my defaut connection.
but I'm getting an error : Undefined variable: marqs (View: C:\wamp64\www\projetSovac\resources\views\home.blade.php)
Here's my controller code
public function index()
{
$marques= DB::connection('sqlsrv2')->table('marque')->get();
$modeles = DB::connection('sqlsrv2')->table('Modele')->select( DB::raw('CodeModele'))->get();
$finitions = DB::connection('sqlsrv2')->table('finition')->select( DB::raw('CodeFinition'))->get();
$marqs = $marques->all(['marque']);
$models = $modeles->all(['CodeModele']);
$Finitions = $finitions->all(['CodeModele']);
return View::make('home')
->with(compact($marqs))
->with(compact($models))
->with(compact($Finitions));
return View('home');
}
and my home.blade.php code
<tr class="filters">
<th><input type="text" class="form-control daterangepicker-field" placeholder="Période d'analyse" disabled ></th>
<th><select class="form-control " disabled>
{!! Form::Label('marque', 'marque:') !!}
#foreach($marqs as $marque)
<option value="{{$marque->codeMarque}}">{{$marque->codeMarque}}</option>
#endforeach
</select>
</th>
Can you help identify the problem?
Thanks
compact($marqs) wants to have a string divining the variable you want to pass to the view. Use: compact('marqs') you can also combine your variables like compact('marqs', 'models', ....etc )
Also you are returning something 2 times now in the function this is not possible.
I would rewrite your function to be like this:
$marques= DB::connection('sqlsrv2')->table('marque')->get();
$modeles = DB::connection('sqlsrv2')->table('Modele')->select( DB::raw('CodeModele'))->get();
$finitions = DB::connection('sqlsrv2')->table('finition')->select( DB::raw('CodeFinition'))->get();
$marqs = $marques->all(['marque']);
$models = $modeles->all(['CodeModele']);
$Finitions = $finitions->all(['CodeModele']);
return View::make('home')->with(compact('marqs', 'models', 'Finitions'));
Assuming the first 6 lines get you the actual data all i changed was the return.
You might want to read up on how to use laravel models
https://laravel.com/docs/5.7/eloquent
I am not sure if u have defined any but it could make your code allot simpler.
I want show the name in a select input form when I select one of their options.
I create the select like that:
<select ng-model="electionEventId" ng-options="option.value as option.name for option in electionEvents">
</select>
And I catch the selected item value with {{ electionEventId }} but I want the name too of this election without realize another request. ¿Anyone can help me?
Thanks
You can bind the entire object in your model:
<select ng-model="electionEvent" ng-options="option as option.name for option in electionEvents">
</select>
My answer seems to be a little too verbose compared to other answers here, but I hope it helps!
I believe that you will have to traverse the electionEvents looking for the option that has value property equal to electionEventId.
For example, add this function to your controller:
$scope.getElectionEventName = function() {
var name;
angular.forEach( $scope.electionEvents, function( option ) {
// Don't process if the name has already been found
name != null && return;
if ( option.value === $scope.electionEventId ) {
name = option.name;
}
});
return name;
};
I haven't tested it, but I'm pretty sure it works!
Unfortunately there's no how to break the loop, so I put that line in the loop function :)
So I have a form on a page, and I am trying to pre load all the old value when the page loads up. But I am having some trouble achieving this for the select object.
I have the following scala code for generating the select:
<select id="networkInterfaces">
<option value="">First Available</option>
#for(interface <- networkInterfaces) {
#if(interface.Name == configs.last.networkInterfaceName) {
<option selected="selected" value="#interface.Name">#interface.DisplayName</option>
} else {
<option value="#interface.Name">#interface.DisplayName</option>
}
}
</select>
And when the page loads it does show that the selected network interface is selected. But the problem is if I change some of my other settings and submit it is returning the options html not the value. Is there a way to select the form value for the select in scala while the page is loading?
Is there something fundamental I am missing? Otherwise I will change the way it is handled to process display name rather than value...
EDIT
As I could not get this to work i changed the value to #interface.DisplayName and then converted it in code on the server. I would like to be able to do it properly but it doesnt seem to work.
Is that correct that <select id="networkInterfaces"> has no name specified?
and than you should use play helper #select and Form object. I think its the better way to handle form values.
#(form : Form[ABean])
#import helper._
#select(
form("configs.last.networkInterfaceName"),
options(networkInterfaces),
'_label -> "Interface Name",
'_default -> "First Available"
)
Further info about helper #select visit :
https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/views/helper/select.scala.html
Try this way.
<input type="hidden" id="groupIdRef" name="groupIdRef" value="#userRecord.groupId">
#input(field=userRecordform("groupId"),'_label -> "") { (id, name, value, htmlArgs) =>
<select id="groupId" name="groupId" #toHtmlArgs(htmlArgs)>
options.map { v =>
#for(g <- groups){
<option value=#g.id >#g.displayName</option>
}
}
</select>
}
// User JavaScript this onload function
window.onload = function() {
document.getElementById('groupId').value = document.getElementById('groupIdRef').value;
}
It is work for me.
I'm tring to implement the classic functionality of this select:elements depends on this other select:selection using Lift, E.g. select the country and get the possible states for the selected country.
The problem that I'm facing is that the "this.myForm.submit()" that I have inside the onchange of the select element is not firing the ajax request. If I use an input type"submit" it works perfectly.
Is this behaivior related with Lift framework? Is there a better way of implementing this kind of functionality using Lift libraries?
The relevant snipped code:
"name=distributionPoints" #> SHtml.select(distributionPoints, Empty, selectedDistributionPoint = _, "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(devices, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
The view html:
<form class="lift:form.ajax">
<select name="distributionPoints" id="the_distributionPoints" onchange="submit()">
<option></option>
</select>
<br/>
Device:
<select name="devices" id="the_devices">
<option></option>
</select>
</form>
The rendered HTML:
<form id="F391649920812YBACEZ" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+"F391649920812YBACEZ").serialize(), null, null, "javascript");return false;">
<div>
Punto de distribución:
<select onchange="submit()" id="the_distributionPoints" name="F630704816482OLP514"></select>
<br />
Equipo:
<select name="F391649920817BLRJW5" id="the_devices"></select><input type="hidden" name="F391649920818HPS35E" value="true" />
<br />
</div>
</form>
[edit]
I finally got the solution. Like Chris mentioned I used ajaxSelect instead of just selects and instead of using setHtml there's a method called JsCmds.ReplaceOptions that does exactly what I was looking for.
You should understand that when using Ajax submit the page is not reloaded. So I would suggest you to use JsCmds.setHtml on server side to "reset" the second select element.
So, in fact the first select is an ajaxSelect which is meant to modify the second one (so it is not concerned by the hidden submit in my opinion). The second select is updated when the first one is changed, using 'selectPoint(s)'
Piece of Scala code
def selectPoint(s):JsCmd = {
selectedDistributionPoint = s;
newDevices:List[String] = findCorrespondingDevices(s);
JsCmds.setHtml("name=devices", SHtml.select(newDevices, Empty, selectedDevice = _, "id" -> "the_devices")) ++ SHtml.hidden(process))
}
"name=distributionPoints" #> SHtml.AjaxSelect(distributionPoints, Empty, s=> selectPoint(s), "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(Nil, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
Piece of template code
<input name="distributionPoints" id="the_distributionPoints" onchange="submit()"/>
<input name="devices" id="the_devices"/>
I'm trying to find a way to get all values and label from drop-down in web page.
With label, I could use:
my #labels = $sel->get_select_options('s');
Return value is array of label in drop-down.
However, there's no equivalent method for getting all values.
Do you guys know how to do this?
As far as in Selenium 1 there is no direct API for this. However you could try this.
Consider a <select> like below.
<select name="mydropdown" id="optionset">
<option value="Milk">Fresh Milk</option>
<option value="Cheese">Old Cheese</option>
<option value="Bread">Hot Bread</option>
</select>
Below is the snippet in Java to retrieve values. You can get the logic from this snippet and implement it in Perl.
int no_of_options = selenium.getSelectOptions("//select[#id='optionset']").length
String option_values[] = new String[no_of_options];
for (int i=0;i<no_of_options;i++){
String value = selenium.getAttribute("//select[#id='optionset']/option["+i+"]/#value");
option_values[i] = value;
}
Hope this helps.