Selenium, select value from listbox without using select in Java - java

How to select a value from dropdown?
My solution is not working:
Select dropdown_month = new Select(
driver.findElement(By.xpath(".//*[#id='BirthMonth']/div[1]/div[2]"))
);
dropdown_month.selectByVisibleText("July");

This is because you're trying to handle <div> element while Select class should be used only with <select> elements.
In your case you might need to use simple click() method as below:
driver.findElement(By.xpath(".//*[#id='BirthMonth']/div[1]/div[2]")).click() // To open drop-down
driver.findElement(By.xpath(".//*[.='July']")).click() // To select required option

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 :)

Can't select the drop-down option

Using the select method I can't select the dropdown
I tried using normal and findelement method and also index selection method
Here am using input value in different file
click(driver,"id",prop.getProperty("state"));
click(driver,"xpath",prop.getProperty("voption"));
and
Index selection method
and
Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");
Expected result :
need to click the dropdown
Actual result :
"stale element reference: element is not attached to the page document"
it showing error message like this
Try locating the WebElement first, then select by visible text.
WebElement dropDown = driver.findElement(By.id("state"));
new Select(dropDown).selectByVisibleText("ANTARCTICA");
If this does not work, but you dont get the StaleElementReferenceException change the selection option to selectByIndex() or selectByValue().
If you get StaleElementReferenceException pointing to line with driver.findElement(...) it means that something on your page has changed so some kind of wait mechanism should be introduced. In such case I suggest to locate the dropDown using FluentWait.

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();
}

How to select an item from a CSS custom dropdown list using Selenium WebDriver with java?

I want to select an item in a CSS dropdown but there seems to be no way to do it, we can take as an example the google hotel review page here how can I select by most recent reviews programmatically through selenium ?
Basically I want to see all the reviews of the hotel sorted by Most recent instead of most useful, but since by default are ordered by most useful I need to switch through the dropdown programmatically.
I've tried this way :
Select select = new Select(driver.findElement(By.xpath("//*[#id=\"gsr\"]/g-lightbox/div[2]/div[3]/div/div/div/div[1]/div[3]/div[2]/g-dropdown-menu/g-popup/div[2]/g-menu")));
select.deselectByIndex(1);
but I'm getting an exception(org.openqa.selenium.support.ui.UnexpectedTagNameException) saying :
Element should have been "select" but was "g-dropdown-menu"
Is there a way to simulate a click on a CSS dropdown element like this with Selenium web driver ?
Analysis:
Selenium Java API: Select.class only suitable for dropdown which use HTML select tag. For dropdown implment by other way, like JQuery dropdown plugin, Select class not support, for such dropdown you need to click on the drop down to make the options to display out, then choose the option you wanted.
Solution:
public void selectSortby(String sortBy) {
// click on dropdown to expand options
driver.findElement(By.xpath("//div[span[text()='Sort by:']]//g-dropdown-button").click();
// choose option
driver.findElement(By.xpath("//g-menu-item/div[text()='"+sortBy+"']")).click();
}

How to select a dropdown value inside a frame in webdriver using java

I am trying to select a dropdown value which is inside a frame. This is the code
This is how we write for a link with in frame but i am unable to select a value from dropdown
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("right"));
WebElement el1 = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Text")));
el1.click();
First, wait for the correct frame (according to HTML code, the name of the frame is main_b)
Next, you don't have a link there (<a> tag), so By.partialLinkText cannot be used. Use By.name("field") instead
Finally, instead of clicking on it, get a Select object: Select mySelect = new Select(el1); and choose one of its options using selectByVisibleText, selectByValue or selectByIndex method
So all together looks like this:
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("main_b"));
Select mySelect = new Select(
wait.until(
ExpectedConditions.elementToBeClickable(
By.name("field")
)));
// Select second option by visible text
mySelect.selectByVisibleText("Bacon Anna");
// Same option selected by value
mySelect.selectByValue("16344");
// Same option selected by index
new Select(el1).selectByIndex(1);
Try below code:
WebElement fr = driver.findElement(By.name("main_b"));
driver.switchTo().frame(fr);
WebElement dropdown = driver.findElement(By.name("field"));
Select sel = new Select(dropdown);
sel.selectByValue("<Value to be selected>");
You can also use wait commands if your page takes some time to load. Hope it helps.

Categories

Resources