How to select an option from a dropdown using Selenium - java

I have this dropdown:
<select class="admin__control-select" data-bind="
attr: {
name: inputName,
id: uid,
disabled: disabled,
'aria-describedby': noticeId
},
hasFocus: focused,
optgroup: options,
value: value,
optionsCaption: caption,
optionsValue: 'value',
optionsText: 'label'" name="product[business_line]" id="N2JWY3F" aria-describedby="notice-N2JWY3F"><option value=""> </option><option data-title="PU - 21" value="425">PU - 21</option><option data-title="PU - 35" value="430">PU - 35</option></select>
The XPath is:
//*[#id="N2JWY3F"]
It has 2 options available: PU - 21 and PU - 35. I want to select the option: PU - 21.
I did this:
First, click on that dropdown:
driver.findElement(By.xpath("//*[#id=\"N2JWY3F\"]")).click();
How can I specify the option that I want? I tried different things and not one worked.

I'm guessing the drop-down isn't being clicked properly; can you check if "/*[#id="N2JWY3F"]" has only one match, and if so, maybe you can experiment with some other attributes:
driver.findElement(By.id("N2JWY3F"));
OR
driver.findElement(By.name("product[business_line]"));
OR
driver.findElement(By.className("admin__control-select"));
If updating the locator does not work, make sure the page is loaded and the drop-down menu is enabled before clicking. You can either wait or make sure the drop-down menu is visible on the page.

To select a value from the dropdown on the DOM you can utilize the Select class in Selenium.
Select drpDown = new Select(driver.findElement(By.id("N2JWY3F")));
drpDown.selectByVisibleText("PU - 21");
Basically first assign the dropdown to the Select class drpDown and then use one of the available methods to get the option you want.

Related

How to get value from nested xpath in selenium

I am trying to test an application with Selenium and Java, what I want to achieve, is the following.
Click on a select box (which works) and then select from a multiple selectoption the one equaling my input. So if my Feature file says dog I want from the options dog to be selected.
My code to select the option after opening the selectbox looks like this:
WebElement xyz = driver.findElement(By.xpath(String.format("//*[#id='xyz']/div[2]/ul/li[text()='%s']",selectElement)));
If I delete /li every option is selected in my console in the browser, so I thought with /li[text()='%s'] I could dynamically set the option name. But I am getting a nosuchelementexcetion, what could be my mistake? If i right 1 instead of text() = '%s' I am being able to select the option
For the select box, you need to create a Select object which targets the select box and then select by value to get the item you want to select. For example:
Select drpDown = new Select(driver.findElement(By.xpath(//pathToElement]")));
drpDown.selectByValue(selectElement);
Hope this helps :)

Selenium Webdriver with Java - Click on an hyperlinked Dropdown Container

I need to click on an element inside a Dropdown container. I've tried several searchs but I haven't been able to find the correct solution. The select method doesn't work, and I still don't know how to work with Selectors when there's no ID, Name or Class related to it. Here's the HTML code:
Account<span class="caret"></span>
<div class="account-dropdown__container">
<ul>
<li>Account</li>
<li>Invite Friends</li>
<li>Zola Store Credit</li>
<li>Registry Settings</li>
<li>Orders You've Placed</li>
<li><a>Log out</a></li>
</ul>
</div>
The first piece of code is a button, but if I put my mouse over it, it will show the Dropdown container that I am talking about. If I put my mouse over it without clicking, it will show the list of the Dropdown Container. (And I would also like to know how to hover an element to show the list without clicking it, because its hidden).
My question is, then: how can I click on Registry Settings?
It doesn't have an ID, nor a class (although it is inside the class account-dropdown__container). I think I can use By.name("Registry Settings"), but since is not visible unless the Dropdown list is open, it won't click and it will show Css Selector not found error. Care to help? Thanks!
Also, I am using Cucumber + Selenium + Java in IntelliJ IDEA, the synthaxis changes just a bit, but it is still different from the codes I tend to find in this forum. Hence, why I am asking for a specific solution to my problem.
You have to make the dropdown visible first.
As in Selenium you can't just hover an element, you will have to do it all in one go.
Check this: How to perform mouseover function in Selenium WebDriver using Java?
Actions action = new Actions(webdriver);
WebElement button = webdriver.findElement(By.class("account-link"));
action.moveToElement(button).moveToElement(webdriver.findElement(By.linkText("Registry Settings")).click().build().perform();
You may have to wait in between for the dropdown to appear. I have not tested the code, you will probably have to fix it before it works.
As you have mentioned when you put your mouse over to a button, it will show the Dropdown container.
Same can be automated with the help of selenium like this : (I am assuming account is a button )
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Account"))).build().perform();
Now your drop-down is expanded or visible in UI, and you want to click on Registry Settings . Since your drop down is not made using Select options tags which are available in HTML. You can't use Select class from Selenium.
You will have to store every elements which are present in drop down to a list. and then based on some condition , you can click on your desire element.
Code:
List<WebElement> options = driver.findElements(By.cssSelector("div.account-dropdown__container ul li"));
for(WebElement option : options) {
if(option.getText().trim().contains("Registry Settings")) {
option.click();
}
}
Hope this will help.

How to click on select options using selenium

I can't click on option element-
website: http://www.oferty.net
When I am trying to select one option in the drop-down list. Unfortunately
I receive the message:
"Cannot click on option element. Executing JavaScript click function
returned an unexpected error"
What did I do wrong with the following code:
Select estateType = new Select(driver.findElement(By.id("ps_type")));
estateType.selectByVisibleText("domy");
Please help.
I quicky check the HTML code of the dropdown you are trying to select and it's not a HTML dropdown. it's custom dropdpown created with CSS and so you can't use the default function provided by Selenium to select Dropdown Value.
<div class="jquery-selectbox-list jquery-custom-selectboxes-replaced-list" style="width: 113px; height: 9em; display: none;">
<span class="jquery-selectbox-item value-998 item-0">rynek pierwotny
</span>
...
</div>
Possible Solutions
Try SendKeys on jquery-custom-selectboxes-replaced-list element and see if it's working, if not
Click on jquery-custom-selectboxes-replaced-list and then scroll to the element you are interested in and click on it.
It's a simulated dropdown list, not native. Selenium Select Class can only work on native dropdown list define by Select Tag. Even there is also embed a native dropdown, but it's hidden, that's why report can't click on option error. And When you operate on the dropdown from page, the embed native dropdown is always hidden, thus actually you not operated on the embed native one.
Try below code:
public void choosePsType(String psType) {
// find the container node of the dropdown list
WebElement psTypeSelect = driver.findElement(By.cssSelector("label[for='ps_type'] + div"));
// click on the selector to expand options
psTypeSelect.findElement(By.cssSelector("span.jquery-selectbox-currentItem")).click();
// choose wanted option
String xpath = String.format(".//span[contains(#class, 'jquery-selectbox-item')][.='%s']", psType);
psTypeSelect.findElement(By.xpath(xpath)).click();
}

selenium webdriver have to find element from dynamic dropdown

I hope that someone could help me solving this.
Have to choose from dynamic dropdown only one which is active, disabled no. Following is part of dropdown.
<option value="1-2639425" disabled="disabled">21/10/2017 16:45 - Felipe Arantes - Josh Emmett (No odds available)</option>
<option value="1-2636744" disabled="disabled">21/10/2017 16:45 - Jim Wallhead - Warlley Alves (No odds available)</option>
<option value="1-2633126">21/10/2017 20:00 - Donald Cerrone - Darren Till</option>`
There are few things which confuses me:
I have to choose only the one(s) option which does not have 'No odds available' in it - they are clickable (those without 'No odds available')
It is not necessary that the one which can be chosen reside on 3rd position (as shown above) - can be first, second, 50th
Actually, I have to choose first clickable one (no matter where reside in option list)
Tried with this code but without any success:
Select dropdown=new Select(driver.findElement(By.xpath("xpath to 3rd option")));
dropdown.selectByIndex.selectByIndex(2);
Thread.sleep(5000);
Please assist.Thank you in advance.
You can get all the options using getOptions() and then get the first option without "No odds available" text or disabled attribute. Then you can use Select to select an option using the value attribute
Also, Select class doesn't get an option as parameter, it gets the parent <select> tag as parameter
Select dropdown = new Select(driver.findElement(By.xpath("xpath to the select tag")));
List<WebElement> options = dropdown.getOptions();
for (WebElement option : options) {
// if (option.getAttribute("disabled") != null)
if (!option.getText().contains("No odds available")) {
String value = option.getAttribute("value");
dropdown.selectByValue(value);
break;
}
}

I am trying the code by JAVA - Selenium for the WebApplication

Can anyone select any item from the drop down list box of "Furniture Type". I got the clicking on the the furniture type by
driver.findElement(By.xpath("(//select)[1]")).click();
But I am unable to select any one text from the drop down list box. By Select class I tried, But I couldn't get the solution.
The url is " http://bangalore.quikr.com/post-classifieds-ads/?postadcategoryid=218 "
The exception I am getting is "no such element exception".
Usually you should first check the path using developer tools or similar browser extension, or record using Selenium IDE. In this case for instance, I don't see any select element on that page: Furniture Type is an input with type=text and the selectable options are input with type=radio. Also (//select)[1] is not a valid xpath.
So you can find it by
WebElement typeBox = driver.findElement(By.id("Furniture_Type_searchBox"));
Since Furniture Type is an input with type=text, you can then focus on it like this:
typeBox.sendKeys("");
That should also open a drop-down, from which you should select a radio button like this (selecting 'Bed Set' for example):
WebElement radio = driver.findElement(
By.xpath("//div[#class='attr-container']//input[#lblval='Bed Set']"));
And you click to select it
radio.click();

Categories

Resources