Selenium Select Hidden Drop Down Menu - java

Good afternoon everyone,
So my issue is I am trying to select fields in drop down menu's that are hidden. Below is the HTML code for one of the said drop down menus.
<div id="mainForm:defectPriorityField" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-helper-clearfix" style="width:120px">
<div class="ui-helper-hidden">
<select id="mainForm:defectPriorityField_input" name="mainForm:defectPriorityField_input">
<option value="1-Urgent">1-Urgent</option>
<option value="2-Very High">2-Very High</option>
<option value="3-High">3-High</option>
<option value="4-Medium">4-Medium</option>
<option value="5-Low">5-Low</option>
</select>
Now I have read there are a couple ways I can do this. Below seeming to be the most popular.
String cssSelector= ...
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
The xpath for the drop down menu is //*[#id="mainForm:defectPriorityField_input"]
I am relatively new to this and am unsure how to generate or get a cssSelector. Please if anyone could point me in the right direction it would be greatly appreciated.

Here's the approach I'd take which is to first click on the drop down menu then select an option:
WebElement dropdown = driver.findElement(By.xpath("//div[#id=\"mainForm:defectPriorityField\"]);
dropdown.click();
WebDriverWait waitForOptions = new WebDriverWait(driver, 3);
waitForOptions.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id=\"mainForm:defectPriorityField_input\"]")));
WebElement option = driver.findElement(By.xpath("//div[#id=\"mainForm:defectPriorityField_input\"]/option[text()=\"1-Urgent\"]"));
option.click();
Please excuse any syntax errors but that's essentially the idea. In the last statement for the option element you can substitute the text "1-Urgent" with a variable or whatever you like.

If an element is Hidden, you should not be using it. As the user would not either.
If it is not then why not use the SelectElement?
SelectElement sel = new SelectElement(driver.FindElement(By.Id("Id")));
sel.SelectByValue("Value");

Related

Selenium webdriver java - Automating Dojo Dropdown - Getting no such element exception

After clicking one hyperlink in my application. There is a Dojo popup appears mentioned below:
From the dropdown I need to select QA/SIT environment value.
HTML code for this is mentioned below:
<input class="dijitReset dijitInputInner" type="text" autocomplete="off" data-dojo-attach-point="textbox,focusNode" role="textbox" tabindex="0" id="dijit_form_FilteringSelect_0" aria-required="true" value="" aria-invalid="false">
Now, I tried below code to select the value from dropdown But I am getting no such element exception.
Select drpCountry = new Select(driver.findElement(By.xpath("//*[#id=\\\"dijit_form_FilteringSelect_0\\")));
drpCountry.selectByIndex(4);
I am getting below No such element exception error.
Can anyone please explain what I am missing here. How to Automate Dojo select dropdown
You can try below code to select your option. First click on drop down arrow present in there
:
String option = "QA/SIT"
driver.findElement(By.xpath("//input[#class='dijitReset dijitInputField dijitArrowButtonInner']")).sendKeys(option ) # Choose a valid xpath if its not correct
optionXpath = "//div[text()='"+option+"']"
optEle = driver.findElement(By.xpath(optionXpath ))
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(""arguments[0].click();",optEle );

Problem with selectors in Selenium Web Driver

I have a problem with some selectors to click in my automatic test in Selenium. My test doesn't see any of the selectors that I used. There is my div which I working with:
<select name="people" id="demo-htmlselect" onchange="selectConfigOption(this.value)" >
<option value="">Choose a selection...</option>
<option value="429" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/tobacco.png"
data-description=""> Tobacco 27 mg/ml </option>
<option value="432" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/menthol.png"
data-description=""> Menthol 27 mg/ml </option>
<option value="435" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/cherry.png"
data-description=""> Cherry 27 mg/ml </option>
</select>
And my ideas (which aren't working):
wd = new FirefoxDriver();
WebElement span = wd.executeScript("return document.getElementById('dd-select');");
wd.findElement(span).click();
//wd.findElement(By.xpath("//div[#class='dd-select']/span[#class='class='dd-pointer.dd-pointer-down'']")).click();
//wd.findElement(By.xpath("value=//*[#id='432']"));
//WebElement register = wd.findElement(By.name('people'));
//wd.findElement(By.partialLinkText("Choose a selection...")).click();
//wd.findElementById("select=//*[#id='429']").click();
Thanks for every advice!
As per the html you have shared the the element is a <select> tag so you have to use the Select class an additionally induce WebDriverWait to choose an option and you can use the following solution:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[#id='demo-htmlselect' and #name='people']")));
Select mySelect = new Select(elem);
//selecting the second item by value 429
mySelect.selectByValue("429");
This drop down is made using select and option tags, so you can use select class from selenium.
Select drop_down = new Select(driver.findElement(By.id("demo-htmlselect")));
drop_down.selectByVisibleText("Menthol");
Or
drop_down.selectByValue("432");
This is not the correct way to select an option from a select box.
See Select
You have to use something like this:
WebElement element = <WEB DRIVER INSTANCE>.findElement(By.xpath(SELECTOR FOR THE SELECT ELEMENT));
Select select = new Select(element);
select.selectByValue(<VALUE OF THE SELECTED ITEM>);

canĀ“t select dropdown option (selenide/java)

I have a little problem selecting options out of drop down lists with selenide (java).
Here's a little snippet of the HTML code and my try to select the option by the value:
HTML snippet
[Java code]
String dateRangeSearchFor = "YESTERDAY";
ElementsCollection ListOfOptions = $(By.id("searchMaskForm:jobSearch_dateRange_input")).$$(By.tagName("option"));
logger.info("selecting option");
for (SelenideElement listElement : ListOfOptions)
{
String valueOfElement = listElement.getAttribute("value");
if (valueOfElement.equals(dateRangeSearchFor))
{
//$(By.xpath("//*[#id='searchMaskForm:jobSearch_dateRange_input']/option[contains(., '"+dateRangeSearchFor+"')]")).setSelected(true);
listElement.setSelected(true); break;
}
}
For some reason the code is not working, neither with the text nor with the index. Any suggestions?
Edit: .click(); and selectOption(); aren't working neither
SelenideElement have method selectOptionByValue(java.lang.String... value)
The piece of code below will help:
String dateRangeSearchFor = "YESTERDAY";
Select select = new
Select($(By.id("searchMaskForm:jobSearch_dateRange_input")));
select.selectByValue(dateRangeSearchFor);
In my case it did.
BTW, if the automation test suite you're creating is a part of automation, that includes functional and load testing, this link will help you to combine those tools in one system, check it out - How to automate Selenium and jmeter testing .
Selenide provides the following methods for a selecting an option in dropdownlist.
selectOptionByValue(value)
selectOption(text)
selectOption(index)
selectOptionContainingText(text)
If you know the index of the element in dropdown menu then you can just use built-in methon selectOption().
It will look like this:
$(CSS-selector).selectOption(index-of-element);
!!REMINDER: CSS-selector MUST point to the <select> element in HTML.
<select class="switch-version" id="switch-version-select" onchange="switchVersionSelect()">
<option value="/v1.x/demo/my_boss_is_in_a_hurry/flexigrid">Flexigrid</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/datatables">Datatables</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap" selected="selected">Bootstrap V3 Theme</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap-v4">Bootstrap V4 Theme</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap-v5">Bootstrap V5 Theme</option>
</select>
In the example of the html code above, to select one of the value options with Selenide simply implement the code below.
public class HomePage {
SelenideElement bootstrapElement = $(By.cssSelector("#switch-version-select"));
public void selecionarDropDown(){
$(bootstrapElement).selectOptionContainingText("Bootstrap V4 Theme");
}
}

How can i deselect an option from a dropdown, when that respective dropdown is not a "multi-select" / java & Selenium

I tried to use the objname.deselectByVisibileText() on multiple dropdowns (select/span) and I get the following error
Exception in thread "main" java.lang.UnsupportedOperationException: You may only deselect options of a multi-select.
How can I clear those respective fields? My method atm looks like this:
public void deselect(String s, String t)
{
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
Select select = new Select(element);
select.deselectByVisibleText(t);
}
Obviously, I need a solution without deselect, as none of them work (byValue, byIndex, etc.) due to the same error as above.
Typically the first option is the default. You can just select it.
select.selectByIndex(0);
If you have a select element that looks like this:
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
Running the code:
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex];
would return the selected option.
Now you got to know the selected index. So, use javascript executor to unselect it

I am trying to automate Goibibo in Selenium using Java

I am trying to automate Goibibo Website in Selenium using Java. After Clicking on Search options there are lot of tabs with BOOK option.
On Inspect, All tabs have same X-Path. How to select one of them?
Following is the Code:
<input type="button" value="BOOK" class="button orange fr fn ft_bookbtn ">
Use indexes
List<WebElement> books = driver.findElements(By.cssSelector("[value='BOOK']"));
books.get(1);
if you want to take the first on you can also use
WebElement book = driver.findElements(By.cssSelector("[value='BOOK']"));
if you want random selection you can do this:
List<WebElement> books = driver.findElements(By.cssSelector("[value='BOOK']"));
int r = new Random(books.length).nextInt();
WebElement book = books.get(r);

Categories