I can't select HTML Dropdown list with my Webdriver method. What was wrong in my code.? Could you give me some hints.
<select>
<option value="32">32</option>
<option value="34">34</option>
<option value="36">36</option>
</select>
public static List<WebElement> chooseSize(Integer size){
WebElement select = findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals(size)){
option.isSelected(); // or .click()?
}
}
return options;
}
There's a support class that can help you with that in WebDriver: "org.openqa.selenium.support.ui.Select".
Here is how you use it:
// First, get the WebElement for the select tag
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
// Then instantiate the Select class with that WebElement
Select select = new Select(selectElement);
// Get a list of the options
List<WebElement> options = select.getOptions();
// For each option in the list, verify if it's the one you want and then click it
for (WebElement we : options) {
if (we.getText().equals(valueToSelect)) {
we.click();
break;
}
}
Select select = new Select(driver.findElement(By.xpath("Xpath_of_Select_Element")));
select.selectByVisibleText("Option_to_Select");
This is the simplest way to select an option from a select drop down
For such cases, I'm using xpath expressions. You'll save a lot of code!
For what you are asking for, this should do (I assume that your xpath is properly targeting the corresponding select):
// Click select first:
// (See http://code.google.com/p/selenium/issues/detail?id=2112)
findElement(By.xpath(DropDown_Article_Size_XPATH_ID)).click();
// Then click option:
String xpathOption = String.format("%s/option[text()='%d']",
DropDown_Article_Size_ID, size);
log.debug("Selecting option by text '{}' using xpath '{}'", size, xpathOption);
findElement(By.xpath(xpathOption)).click();
By the way, I don't get why your chooseSize returns the list of all options. You should probably rename the method to something meaningful (getOptionsBySize, for example, if this is what you want).
Bit modification it works for me, thanks a lot such a simple code it does the job.
Select select = new Select(driver.findElement(By.name("Status_operator")));
select.selectByValue("=");
Have you tried setSelected()? isSelected() is a getter so it won't change anything.
If you are using Selenium2 you have to use option.click().
I'm afraid that there's an issue with ChromeDriver and Select. Tested on Chrome for MacOSX, .click() and .isSelected() don't work. The same code in FireFox, works as expected. Is there anything different between both browsers?
List<WebElement> opciones = select.getOptions();
for(WebElement el : opciones){
System.out.println("Elemento disponible: ["+el.getAttribute("value")+"]["+el.getText()+"]");
//Select actual option
el.click();
if(el.isSelected())
System.out.println("Selected: ["+el.getAttribute("value")+"]["+el.getText()+"]");
}
you can do
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
selectElement.sendKeys("34")
to select 34
its that simple. Sendkeys is a very useful method in webdriver and has different implementations for different kind of objects i.e. for a textbox Sendkeys would type in the text, while for a select element it would select element.
I have even read that for a file upload field you can do sendkeys to enter the file path.
cheers
Shrikant
Related
I want to select value by text from a dynamic non select dropdown. I did some research and I found this code:
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("/html/body/div[1]/div[2]/div/div/div"));
for (WebElement option : options){
if (option.getText().contains(text)){
option.click();
break;
}
}
Basically it put the dropdowns options into a List element, and run through in a for loop, and if the options contains text, click it and break the loop. However it is not working with this type of dropdown:
Snapshot:
Can you suggest what can I do?
Snapshot of Specific value:
Note: The page is only available via private vpn, so I cannot share it.
To select the value by text Teszt_5 from a dynamic non Select dropdown you can use the following locator strategies:
String text = "Teszt_5";
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("//div[#class='mat-autocomplete-panel mat-autocomplete-visible ng-star-inserted' and starts-with(#aria-labelledby, 'mat-form-field-label')]//mat-option//span[#class='mat-option-text']"));
for (WebElement option : options){
if (option.getText().contains(text)){
option.click();
break;
}
}
References
You can find a couple of relevant detailed discussions in:
Automating jQuery based bootstrap dropdown using Selenium and Java
How to select a DropDown from a list but HTML don't have select Tag using Selenium Java
I tried to click on an element using CSS selector and later Xpath. But failed both. Can anyone help me in solving the issue. Below is the xpath I provided.
Xpath: //*[#id="content"]/div/div[1]/ul/li[3]/div[2]/div/button
Html: Select or search a country in the list... Bahrain
I am new to Selenium and don't have prior experience in automating applications. Should I stick on using xpath or should try using some other locators?
You can either use JavascriptExecutor:
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Or
Get the xpath of the WebElement. Put them in the List (Java Collection)
List<WebElement> lst = xpath ;
for(WebElement we:lst){
if(we.getText().equalsIgnoreCase("Bahrain"))
we.click();
}
}
You can use the code below to search for the desired text and click the element.
String searchText = "Bahrain"; // you can parameterize it
WebElement dropdown = driver.findElement(By.id("content"));
dropdown.click(); // assuming you have to click the "dropdown" to open it
List<WebElement> options = dropdown.findElements(By.tagName("li"));
for (WebElement option : options)
{
if (option.getText().equals(searchText))
{
option.click(); // click the desired option
break;
}
}
I want to click on drop down button and select one among one and click on it.I tried by many ways but it is not working. Attached image of html code, Anyone help me regarding this.
Below is my code which i tried
driver.findElement(By.id("homepages_dropdown")).click();
/*List<WebElement> allElements = driver.findElements(By.xpath("//*[#id=\"homepages_item_AccountManagerDashboard\"]"));
for (WebElement element: allElements)
{
System.out.println(element.getText());
}*/
Just use "Select" to create an object that handles dropdowns etc. You can use "Select" to select element by it's text, index and a bunch of other options. It's the easiest way to handle such elements as dropdown and comboboxes.
new Select(driver.findElement(By.id("homepages_dropdown"))).selectByVisibleText("Your option's text");
This isn't the sharpest way to do it, but if you can't get developers to make it a specific select element via HTML it may have to do. I have stuff like this in my solution where there are custom dropdowns.
You can start by creating a dropdown element and then click a single record in it if that's what you're trying to do, in your case it looks like it is the span within the highlighted button element, so you can do something like the following:
driver.findElement(By.id("homepages_dropdown")).click();
driver.findElement(By.xpath("//li//a[#id='homepages_item_AccountManagerDashboard']").click();
The above statements will click the dropdown then click the element with the id of homepages_item_AccountManagerDashboard.
You can also create a function that clicks any li of based off of a parameter you pass into a method you create, consider this if you may select many options in your dropdown.
public void SelectItem(string itemText)
{
var dropdownElement = driver.findElement(By.id("homepages_dropdown"));
var selectionItem = driver.findElement(By.xpath("//li[text()='" + itemText + "']");
dropdownElement.Click();
selectionItem.Click();
}
This is only really a valid option if you are working through the Page Object Model, but it's the best way to make things work consistently long term.
To click on Dropdown button and Select the Option with text as Analytics you can use the following code block :
driver.findElement(By.xpath("//div[#id='homepages']/button/span[#id='homepages_dropdown']")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='homepages']/ul/li/a"));
for (WebElement element : allElements)
if(element.getAttribute("innerHTML").contains("Analytics"))
{
element.click();
break;
}
System.out.println("Option with text as Analytics is selected");
I found solution myself,Below code worked
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("homepages_dropdown")));
// drop down
driver.findElement(By.id("homepages_dropdown")).click();
Thread.sleep(2000);
// selecting configuration
driver.findElement(By.id("homepages_item_ConfigurationHomepage")).click();
Thread.sleep(2000);
I have tried different ways, but not able to get any success. Please help me to find some solution for this problem.
I am testing an application having a page like this.
Please refer to this page and help me to select values from dropdown given above page.
BTW with the help of following lines, I am able to click on dropdown, but then not able to select any value using different techniques.
WebElement source = driver.findElement(By.cssSelector("#step_language > div.well.well-lg > div > div:nth-child(2) > div > div.mars_chosen_container.clearfix"));
source.click();
Try this way.
driver.get("https://www.marstranslation.com/place-order");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a/span[contains(text(), 'English')]")).click(); //Click on dropdown using xpath locator.
WebDriverWait wait = new WebDriverWait(driver, 15); //Use explicit wait method for find an element
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//ul/li[contains(text(), 'Arabic')]"))));
driver.findElement(By.xpath("//ul/li[contains(text(), 'Arabic')]")).click(); //After explicit wait, click on Arabic option from dropdown using xpath locator.
Try this snippet
WebElement TargetLanguage = driver.findElement(By.cssSelector("#targetLanguageId_chosen>ul>li>input"));
TargetLanguage.click();
Thread.sleep(3000);
// Gets the target languages in the List
List<WebElement> LangElements = driver.findElements(By.cssSelector("#targetLanguageId_chosen>.chosen-drop>ul>li"));
for(WebElement t : LangElements)
{
if(t.getText().equalsIgnoreCase("Arabic"))
{
t.click();
}
}
Here as an example I clicked on Arabic. You can replace the same with your required language and use it.
Hope it works for you. If anything goes wrong please feel free to ask.
I prefer to write functions for things like this since they will likely be reused.
public static void SelectSourceLanguage(String language)
{
driver.findElement(By.cssSelector("a.chosen-single")).click(); // click the dropdown
driver.findElement(By.xpath("//ul[#class='chosen-results']/li[contains(.,'" + language + "')]")).click(); // click the language
}
Then you can call it like
driver.get("https://www.marstranslation.com/place-order");
SelectSourceLanguage("Hindi");
My code is below, I am having problems with 'Production Plan', I need to be able to click the Production Plan link but it doesn't work.
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div[4]"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
//System.out.println(we.getText());
links.add(we.getText());
System.out.println(links);
if(we.getText().contains("Production Plan")) {
we.sendKeys("Production Plan");
we.click();
}
Your WebElements in ddOpts list aren't an anchor tags but divs. I don't know how does the page look, but it seems you might thought another xpath. Something like:
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div/a[4]"));
or
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div[4]/a"));
Or maybe if it is a select option, use a Select object
Select mySelect = new Select(driver.findElements(By.xpath("html/body/div[4]")));
mySelect.selectByVisibleText("Production Plan");
see answer of this question:
How to select an option from a drop-down using Selenium WebDriver with Java?
I didn't understand why your trying to sendKeys().
But If you are trying to click on a link, following will work :
WebElement link = driver.findElement(By.PartialLinkText("Production Plan"));
link.click();
or
You can also try with Explicit wait :
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.PartialLinkText("Production Plan"))).click();