WebDriver -- Selenium I can not select the dropdown option - java

I can not understand what the problem is, tried different ways of choosing, I give examples below...
1. You must select or click a selection.
2. Select or click the desired language.
But WebDriver does not see, does not find these elements, but in DOM they are.
You need to go to LinkedIn profile, settings, language selection.
//Before select dropdown.
WebElement language = driver.findElement(By.id("setting-select-language"));
language.click();
//Select dropdown.
Select make = new Select(driver.findElement(By.name("selectLanguage")));
make.selectByValue("en_US");
//Or
Actions act = new Actions(driver);
//XPath of dropdown.
act.moveToElement(driver.findElement(By.name("selectLanguage"))).click().perform();
//XPath of option in the dropdown.
act.moveToElement(driver.findElement(By.xpath("//*[#id='setting-select-language-content']/form/div/select/option[16]"))).click().perform();

focus = true focus = false
When you click on select it changes focus = true

I believe you locator for the select is incorrect, try something like the below code:
WebElement language = driver.findElement(By.id("setting-select-language"));
language.click();
Select make = new Select(driver.findElement(By.name("//*[#id='setting-select-language-content']/form/div/select")));
make.selectByValue("en_US");

Related

Selenium Java- Drop down selection where Style- display is none

When I try to select a drop down with Style display: none;
option 1:
WebElement sysDropDown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddlFeedStatus"));
Select sDropdown = new Select(sysDropDown);
sDropdown.selectByVisibleText("01 - Quarantined");
The above code renders error:
element not interactable: Element is not currently visible and may not
be manipulated
Option 2:
WebElement hiddenWebElement =driver.findElement(By.xpath("//select[#name='ctl00$ContentPlaceHolder1$ddlFeedStatus']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
Option2 recognizes the drop down but unable to select an item from the drop down.
Any help would be appreciated.
Tried few options i see in the site but didtn't help much
You could try altering the style attribute using Javascript, as such:
hiddenWebElement = driver.findElement(
By.xpath("//select[#name='ctl00$ContentPlaceHolder1$ddlFeedStatus']"));
((JavascriptExecutor)driver).executeScript(
"arguments[0].style.display = 'block';", hiddenWebElement);
After that, you can try a Javascript click or regular click.
Could you right click -> Inspect and see if it is really a dropdown? If drop down is visible and still getting this error, it might not an html dropdown. It may look like one but can only confirm if you inspect or look at code.
If element is an html drop down, use the following code to see if it works:
WebElement sysDropDown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddlFeedStatus"));
Coordinates coordinate = ((Locatable) sysDropDown).getCoordinates();
coordinate.onPage();
coordinate.inViewPort();
Select sDropdown = new Select(sysDropDown);
sDropdown.selectByVisibleText("01 - Quarantined");

Unable to select the elements in the language selector of Gmail homepage

I was just playing around automating gmail website using Selenium with Java and I was stuck at the language selection drop down in gmail home page. After selecting the dropdown, I am not able to select any specific language from the listbox as I am getting element not visible exception. How can we select elements from such list boxes? I have already tried using many general techniques like Actions, explicit wait etc.
After login the following code will, as an example, select language Dansk. Important here is to use the Selenium Select class. For documentation see: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/Select.html
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement next = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//content/span[contains(text(),'Next')]")));
next.click();
WebElement settings = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'aos T-I-J3 J-J5-Ji')]")));
settings.click();
WebElement settingsmenuchoice = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(#class,'J-N aMS')]")));
settingsmenuchoice.click();
WebElement select = wait.until(ExpectedConditions.elementToBeClickable(By.id(":m4")));
select.click();
Select languageDropdown = new Select(select);
List<WebElement> Options = languageDropdown.getOptions();
for(WebElement option:Options){
if(option.getText().equals("Dansk")) {
option.click();
}
}
I tried to do the same as you, I succeed by changing the language but also I couldn't choose the language I want. here is the method I used:
driver.findElement(By.xpath("//div[#class='u7land']")).click();
driver.findElement(By.xpath("//div[#class='ry3kXd Ulgu9']")).click();
driver.findElement(By.xpath("//div[#class='OA0qNb ncFHed']")).click();

Drop down button is not responding with selenium in java

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

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.

Webdriver Dropdown List can't select/choose

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

Categories

Resources