I'm new to Selenium WebDriver. I'm testing a drop down list. Here is the code I used to select an item from the drop down.
Select dropdown = new Select(driver.findElement(By.xpath("//select")));
dropdown.selectByValue("FEM");
This is working fine, but what I need is to get selected item as a text. For an example, under the value = FEM , the text displays is female. I need to get the text as Selected value is female.
I've searched some articles and none of this worked. Please help. :)
You can use element1.selectByVisibleText(value); if you want to set the option using text instead of value
If you want to get the value use element1.getAllSelectedOptions().get(0).getText()
or element1.getFirstSelectedOption()
Select has getFirstSelectedOption() method. From there you can use getText()
Select dropdown = new Select(driver.findElement(By.xpath("//select")));
dropdown.selectByValue("FEM");
WebElement option = dropdown.getFirstSelectedOption();
String text = option.getText();
instead of using dropdown.selectByvalue("FEM")
use dropdown.selectByVisibleText("FEM")
Related
I am facing a problem to select an option from the drop down.
The site is https://uk.farnell.com. There is an All drop down just before the Search text box. I want to select "Ceramic Capacitors" from the drop down.
I tried in may ways like using
Select sel = new Select(locator)
JavascriptExecutor
Actions
List
But none worked.
The image displays All has been clicked and Ceramic Capacitors is highlighted which I want to select
xpath for All - .//div[#id='catContainer']
xpath for Ceramic Capacitors - .//option[text()='Ceramic Capacitors']
Select by 3rd index or by visible text.
driver.get('https://uk.farnell.com/')
Select(driver.find_element_by_id('categoryIdBox')).select_by_index(3)
Select(driver.find_element_by_id('categoryIdBox')).select_by_visible_text("Ceramic Capacitors")
Import
from selenium.webdriver.support.select import Select
Please try with below code:
WebElement dropdownlist=Driver.findElement(By.id("categoryIdBox"));
Select listbox = new Select(dropdownlist);
listbox.selectByVisibleText("Ceramic Capacitors");
Can you try the below code it might help to resolve your issue
WebElement element = driver.findElement(By.xpath("((//div[#id='catContainer']//following-sibling::select))"));
Select selectOption = new Select(element);
selectOption.selectByIndex(3);
WebElement option = selectOption.getFirstSelectedOption();
* This is used just to verify the selected option
System.out.println(option.getText());
Please use the below code:
Select options = new Select(driver.findElement(By.xpath("//select[#id='categoryIdBox']")));
options.selectByVisibleText("Ceramic Capacitors");
I am trying to scroll inside of an element using selenium but not able to succeed so far.
I tried some solutions I found on the web but without success.
I would like to select the last user from the following list.
Is anyone knows how can I do that? Notice that all the elements in the list have the same locator.
Thanks
I hope this would do the trick:
Select select = new Select( driver.findElement(by) );
select.selectByIndex( select.getOptions().size() - 1 );
Please try with below code. If possible please share the test URL then I will replicate it from my side.
//get all the options from the dropdown list-
List<WebElement> allOptions = driver.findElements(By.xpath(""));
Actions action = new Actions(driver);
//select last user from the list
action.doubleClick(allOptions.get(allOptions.size()-1)).perform();
I want to click on an item by its visible text inside a specific path
here is the code
I want to click on these option tag by its value or the inner text based on its path as these values (date picker) are repeated in other sections
WebElement x=driver.findElement(By.xpath("//select[#class='calendars-month-year']/option[1]"));
String y=x.getAttribute("innerText"); // =1288
if (y.equals("1288"){
x.click();
}
Or directly
driver.findElement(By.xpath("//select[#class='calendars-month-year']/option[1]")).click(); //this will click on the first option
I think you are trying to select an option by its visible text value. You can use the following code to do the same:
Select sel = new Select(driver.findElement(By.xpath("//select[#class='calendars-month-year']")));
sel.selectByVisibleText("1289");
You can use By.xpath("//option[contains(.,'1290')]") or By.xpath("//option[text() = '1290']")
I write a script on Java for Selenium WebDriver, and I have a problem with selected from dropdown menu.
Here's my locator:
new Select(driver.findElement(By.id("FormElement_select_68_input_input"))).selectByVisibleText("Image");
Here's an error: http://prntscr.com/7jul03
Here's HTML code: http://prntscr.com/7jvou6
Need to select "Image" from this menu, but have an error.
Before I had the error like this, I can't upload file, it was because I need to switch to frame(0).
But here I don't know why I can't select menu "Image" from DropBox.
Your ID is dynamic, so you can't use it. Select will not work in your case, you just need to use two clicks
WebElement dropdown = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/input"));
dropdown.click();
WebElement element = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/div/ul/li[text()='Image']"));
element.click();
It looks like the element id you're looking for"FormElement_select_68_input_input" doesn't exist in your html, your code sample shows "FormElement_select_283_input_container" as the select box element. Try this:
Select droplist = new Select(driver.findElement(By.Id("FormElement_select_283_input_container")));
droplist.selectByVisibleText("image");
Because it is not Select tag.
Try with below logic
WebElement div = driver.findElement(By.cssSelector("div[id*='FormElement_'] > div > div"));
div.click();
WebElement li = div.findElement(By.xpath(".//ul/li[text()='Image']"));
li.click();
As per HTML code screen, i am expecting Select class (selectByVisibleText etc) does not work. can you do one thing, try to click on required option directly. (may be click on "//div[#class='selectbox-wrapper']/ul/li[#class='selectbox_li'][contains(text(),'Image')]" , check one is it correct or not in firepath)
Let me know the result.. if it does not work, as said above you need to click on that input dropdown box and need to click on that Image.
Thank You,
Murali
I am using selenium-RC for my automation with java (Eclipse Kepler) . I am facing some issues with a selection of a option in a drop down list which is not in select tag it is in span.
I want to test a page where their is a drop-down list for selection of city name. the drop-down appears only when I give some value e.g for "Bangalore" I have to type "ban" so the drop-down appears and then I select the city Bangalore either with "mouse click" or either with "down-arrow and enter key" but when I run my selenium rc script it fails after typing "ban" the drop-down doesn't appears. i tried using both xpath and id in select command,click command. I am stuck over here please please someone help me to solve this issue.I think it is dynamic and based on JavaScript functions.One more ..The next two drop down are depended on the first drop-down(i.e the second and third drop-down are hidden and first drop-down is shown by_default.)
I am sending the link of page where i am trying to test script: https://pizzaonline.dominos.co.in
I tried using following commands:-
1:
w.click("//*[#id='homedeliveryform']/div[1]/span/a/span[1]");
w.click("//*[#id='ui-active-menuitem']");
2:
w.type("//*[#id='homedeliveryform']/div[1]/span/input","ban");
w.click("//*[#id='ui-active-menuitem']");
3:
w.select(" id=combobox", "value=BANGALORE");
4:
w.type(" id=combobox", "value=BANGALORE");
I've had a similar problem. This is how I solved it:
Find the arrow button in the combobox and click it, the dropdown list will appear.
The dropdown list doesn't really have anything to do with the combobox, it is a separate list wich is just displayed in a correct position so that it looks like it belongs to the combobox. The dropdown list is actually an <ul>-element, which doesn't seem to be very easily found. Anyway there are many <li>-elements with <a>-elements in them, and those hold the texts.
As i can see you are trying to select a webelement(Bangalore) from a dropdown which is not visible till the time you click on it. Here are the ways by which you are able to select a hidden web element.
1st way: it is not the problem to click any element using the same js. As you know how to get any option the last actions remaning is to perform a click. This should work for you:
WebElement hiddenWebElement =driver.findElement(By(..selector of the element....));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
2nd way:
String cssSelector= ...//i gave them in your previous question
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
3rd way: using actions builder, advanced user actions API. You can read about it here And code will be smth like that:
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();
Hey thank you so much for the help. I solved this by
w.keyPressNative(String.valueOf(KeyEvent.VK_B));
w.keyPressNative(String.valueOf(KeyEvent.VK_A));
w.keyPressNative(String.valueOf(KeyEvent.VK_N));
w.keyPressNative("40"); // down arrow key
Thread.sleep(4000);
w.keyPressNative("10"); //Enter key
In the below scenario what I did is...(dropdwon is in the table)
clicked the cell
get all cell values from the table(probably the list will be configured as table or list .. here it is table)
traverse through all the cells.
click on the required value based on the cell value.
driver.findElement(By.id("ms__id3")).click();
WebElement table = driver.findElement(By.className("combo-list-table"));
allRows = table.findElements(By.tagName("tr"));
for (WebElement row : allRows)
{
cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells)
{
if(cell.getAttribute("text").equalsIgnoreCase("Business"))
{
cell.click();
}
}
}
Hope this will help...:)