How to use selenium webdriver to select an option of Bootstrap select dropdown menu? - select

I've to select a country from dropdown of https://www.parcelhero.com but when I use the below code for it, sometimes it works sometimes not, giving error of Element not found(xpath("//*[#id='dvQuoteFrom']/div/button"))
driver.findElement(By.xpath("//*[#id='dvQuoteFrom']/div/button")).click();
Thread.sleep(4000);
WebElement txt = driver.findElement(By.xpath("html/body/div[14]/div/div/input"));
txt.sendKeys("Great Britain");
List <WebElement> InnerDropdown1 =driver.findElements(By.xpath("//*[#class='active']"));
for(WebElement option1 : InnerDropdown1)
{ System.out.println(option1.getText());
if(option1.getText().contains("Great Britain")) {
option1.click();
break;
}
}
When I used
WebElement txt = driver.findElement(By.className("bs-searchbox")); then also I got the uable to find element error.
Please help me to select a country of my choice from the country dropdown?

Try doing this. I've changed a few of your locators and tested this on the page you mentioned.
public void foo() {
driver.get("https://www.parcelhero.com/");
//This will be your dropdown button. This needs to be clicked first.
driver.findElement(By.xpath("//button[contains(#data-id,'ConsignorAddressCountryId')]")).click();
//These are your input boxes. I found 3 input boxes on the same page having the same identifiers. We dont want to rely on using index based xpath so here we'll get all instances of input boxes.
List<WebElement> elems = driver.findElements(By.xpath("//*[contains(#class,'bs-searchbox')]//input"));
for (WebElement elem : elems) {
//Here we check if the input box is visible. If I'm not mistaken, there will only be 1 visible input box at a time because you have to click on the dropdown button first.
if (elem.isDisplayed()) {
//If visible, just enter your country of choice
elem.sendKeys("American Samoa");
//Assuming you always enter exact match string for your test, you can use the org.openqa.selenium.Keys for sendKeys()
elem.sendKeys(Keys.ENTER);
break;
}
}
}

Related

How to validate Text mesh pro input field text in unity

I have tried to use text mesh pro input field in my project but I have face one series issue with that. i.e If try to validate empty or null text in input field it fails. For example when user without typing any text in tmp input field and click done button I have set a validation like not allowed to save null empty values but when user click done button without typing any text, those validations are fails. Please suggest any idea to fix this issue. Thanks in advance.
Here is the code I have tried :
var text = TextMeshProText.text; // here "TextMeshProText" is 'TMP_Text'
if(!string.IsNullOrEmpty(text))
{
//do required functionality
}
else
{
// Show alert to the user.
}
I have set the validation like this but without giving any text click on done button it fails null or empty condition and enter in to if.
I found the problem. It fails because you use TMP_Text instead TMP_InputField.
Note that: Use the code for TMP_InputField; not for TMP_Text that is inside it as a child.
Change your code to this:
TMP_InputField TextMeshProText;
...
public void OnClick ()
{
var text = TextMeshProText.text; // here "TextMeshProText" is 'TMP_InputField'
if (!string.IsNullOrEmpty(text))
{
//do required functionality
}
else
{
// Show alert to the user.
}
}
I hope it helps you

Protractor unable to select dropdown option

Hi I have been doing protractor test and I'm having a problem with my tests. My ionic app do have a drop down having a model name and I tried to access it using the model name and it works but the problem is it can not select the exact option that i need to select from that dropdown option. It selects only the first one? I wrote the protractor syntax like this.
element(by.model('generalPowerOfAttorney.grantorGeneralPowerOfAttorneyForm.region')).$('[value="59"]').click();
But this code selects not the value 59 rather value 0 which is the default option. Is there anyone who could help me?
You should add the html source to facilitate the answer.
You can use the filter method in order to get the correct element clicked.
var elements = element.all(by.model('generalPowerOfAttorney.grantorGeneralPowerOfAttorneyForm.region'));
elements.filter(function(elem, index) {
return elem.getText().then(function(text) {
return text === 'value="59"';
});
}).then(function(filteredElem){
if(filteredElem[0] !== undefined){
filteredElem[0].click();
}
else {
throw 'element not found'
}
});

How to get and display dialog textbox values in report format in GWT?

I am writing one small dialog box with multiple textboxes, and it contains input fields. I would like to show user's input values like a report or something for all entered values in a list in another dialog.
Here is my code snippet where I set label when user inputs values and presses Button. But when user adds some more additional values in a textbox, would it be possible to be able to see the already added values listed somewhere?
okAndMore = new Button("Add & More");
okAndMore.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String name = nameBox.getText();
if (name.length() > 0) {
items.add(name);
statusLbl.setText(name + " added");
}
nameBox.setText("");
nameBox.setFocus(true);
}
});

Unable to uncheck a checkbox within a combo box

I have a combo box which contains two List Items. Each list item consists of a checkbox and a text message.
Now, I am using a function which looks something like this to uncheck all the checkbox's within the combobox.
public ApplicationReports UnSelectAllCheckBox()
{
int i = 0;
ComboBox someVariable= Application.Library.GetFromWindow(Application.Configuration.LoginWindow.Title).OfType<ComboBox>("corresponding Automation Id");
foreach (ListItem casino in someVariable.Items)
{
someVariable.Item(i).UnCheck();
i++;
}
// someVariable.Item(0).UnCheck();
return this;
}
I am able to uncheck the second checkbox using this approach but not the first one. Unable to identify
what is the problem when it is working fine for the second list item. I am using the recently released version of
white framework. "someVariable" is not a problem. I checked its retrieving the correct combo box while
debugging.
Try this instead:
foreach (ListItem casino in someVariable.Items)
{
casino.UnCheck();
}
No need to use i and make your own loop iterator.

setting a default text to a GWT ListBox

I am trying to create a ListBox using GWT. I am using UiBinder to create the field.
I would like to set a default text on the list box and when a user clicks on the box, it should show me the list items. Once again, if user has not selected any option, it should show me the default text again.
Any way to do this either using Uibinder or some ListBox methods?
If I understand correctly you want a value to show but when the user clicks on the list it disappears and shows you the list items?
As far as I know there is no option to that natively.
What you can do is add the first item to hold your default value.
You can do this grammatically by using addItem in code or using:
<g:Listbox>
<g:item value="-1">Default text</g:item>
</g:Listbox>
works with gwt 2.1+
The value can still be selected.
You can choose to ignore it or add an attribute "disabled" with value "disabled" to the option element:
listbox.getElement().getFirstChildElement().setAttribute("disabled" ,"disabled" )
hope it helps a bit :)
You can also use a renderer to control what is shown if 'Null' is selected.
(Inspired by: How do I add items to GWT ListBox in Uibinder .ui.xml template ?)
private class SimpleRenderer implements Renderer<T>{
private String emptyValue = "Select a value";
#Override
public String render(T val) {
if(val == null) {
return emptyValue;
}
return val.toString();
}
#Override
public void render(T val, Appendable appendable) throws IOException {
appendable.append(render(val));
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
}