I'm trying to automate the drop down field value using c# selenium. I use the following code snippet for select the drop down value and as well as the option.
Code
But I'm not able to click that option .If I trying to write a click functionality for the selected option I'm getting error.
Cannot click on option element.Executing javascript function returned an un expected error,but no error could be returned from IE's
javascript Enginge. .
Please refer the following Screen shot for the Error reference .
Error reference
How can I change the value in dropdown using selenium c# ?
I'm not familiar with C# but this is how you'd do it in java. I believe the syntax is similar so maybe someone can translate it or something?
List<WebElement> dropdown = driver.findElements(By.cssSelector("Your css/xpath/whatever here");
for (int i =0;i<dropdown.size();i++){
WebElement ele = dropdown.get(i);
String textOfElementYouWantToClick = ele.getText();
if(textOfElementYouWantToClick.contains("Value you are looking for")){
ele.click();
System.out.println("Selecting the value you chose");
}
}
Try using the SelectElement class. It has methods for handling dropdown fields.
https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_SelectElement.htm
Have you tried not using javascript to click selected option?
There is Select selenium class to handle dropdowns:
Select myDropdown = new Select(dropdownWebElement);
myDropdown.selectByVisibleText('Value to select');
The other reason for it might be the dropdown is rendered with some framework that keeps actual Select hidden and what you see on screen is actually items from very different element in your DOM.
Related
I am very new to Selenium. On our webpage developers have used jQuery chosen select to fill dropdown. What I want to do is pass specific text and select matching text I entered.
So I tried this:
[FindsBy(How = How.XPath, Using = "//div[#id=MyDrpdown_chosen]//div[#class='chosen-drop']//div[contains(#class,'chosen-search')]/input"), CacheLookup]
private HtmlElement _selectItem;
_selectItem.SendKeys("Banana");
Update 1
Here is screenshot of source inspection in debugger tool
But I get error that it couldn't find matching element. Can someone guide me?
//div[#id=MyDropdown_chosen]
has to be
//div[#id='MyDropdown_chosen']
(you are missing the single quote)
UPDATE 1: Just change your dropdown id
like this : MyDrpdown_chosen
[FindsBy(How = How.XPath, Using = "//div[#id=MyDrpdown_chosen]//div[#class='chosen-drop']//div[contains(#class,'chosen-search')]/input"), CacheLookup]
private HtmlElement _selectItem;
_selectItem.SendKeys("Banana");
If your code is faster than the results are updating you can have a problem finding or interacting with your element. You should make sure you wait enough time for the list to update.
Also note the typo and the missing quotes around MyDrpdown_chosen.
I'm trying to select a value from a dropdown list but I'm getting an ElementNotVisibleException error.
new SelectElement(driver.FindElement(By.CssSelector(select.selectText[name=ADT]))).SelectByValue("2");
And you can see the website that I try to select from dropdown list is
Css of the website
I believe there something to do with jQuery as well but I'm not sure..
Thanks.
You can try something with JavaScript. For example, if you have a hidden button, you can click that button in this way:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", driver.FindElement(By.Id("loginLink")));
See if you can apply the same thing for your dropdown element.
Hope it helps.
Don't you just miss a quotation mark there?
css=selector.class[attribute='value']
In your case:
select.selectText[name='ADT']
or ""
Please try this :
new Select(driver.FindElement(By.CssSelector(select.selectText[name='ADT']))).SelectByValue("2");
You'll need to wait until the element is visible using an explicit wait.
wait.until(ExpectedConditions.visibilityOfElement(By.cssSelector("select.selectText[name=ADT]")
I have a WebElement in which cannot be found in IE 10 but works in FF 47.1 and Chrome 51.
[FindsBy(How = How.XPath, Using = "Foo's XPath")]
IWebElement Foo = null;
At runtime I have also called
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("Foo's XPath")));
But am receiving a NullReferenceException for IE10 Only. I have even tried a sleep as well. Can someone please advise me what else I can do b/c my program has to run IE.
As of the time this question was asked we have upgraded to IE 11 but am still seeing the exact same issue. I can't provide specific html code but I want to elaborate a little on the webelement foo.
The radio button which I have called "foo" is wrapped in the following
div
table
tbody
tr
td
input id = "foo"
When I call the xpath (I have tried id and css selector as well)
.//*[#id='foo_rblArgType_4']
the application itself defaults to the 1st value of the radio buttons and I can't seem to get it to select the 4th value which I need. Can anyone offer advice or suggestions of something else I can try?
Depending of what elements are siblings you can have selectors like:
//tr[4]//*[contains(#id, 'foo')]
//td[4]/*[contains(#id, 'foo')]
//*[contains(#id, 'foo')][4]
You can also use the entire id if is not changing by replacing [contains(#id, 'foo')] with [#id='foo']
If none of this are working then please provide the structure for all 4 inputs.
Try identifying element by id.
[FindsBy(How = How.Id, Using = "foo")]
IWebElement Foo = null;
Also make sure that the element is not hidden. Also try increasing the size of the element as sometimes web-driver is not able to locate element.
If you still have issues performing any operations on radio button, try using JavaScriptExecutor.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html
I am attempting to access a form within a RadWindow. The web page uses window.radopen() to generate an ASP.NET popup. I need to access that popup, edit it, and click a button. Is there a way to do this using Selenium WebDriver?
Specifically, the radwindow contains a textarea with an id of "txtEntries" and a button with and id of "btnAccept". I have tried finding the textarea first, as below, with no luck.
I am currently attempting:
state = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.CssSelector("div#radWindow #txtEntries"));
});
With failed results.
Yea, there's a way.. Since RadWindow is not an actual window, it makes it significantly easier.
First, use CSS and have a parent selector. Something like,
div#radWindow
Then just add the elements you want to find to that. e.g.
input[type='text'].someclass
then just concatenate them, so it turns into this -
div#radWindow input[type='text'].someclass
which translates in CSS to "first find a div with id radWindow, and find an input that is a descendant of the div, with the type attribute that equals text that has someclass attached.
I am trying a select a text from the Dropdown Menu using Selenium webdriver in C#
It is working perfectly with Chrome browser, but not with Firefox. Could any one help me to fix this.
The code I am using is given below.
public void SelectCountry1(string country)
{
var countryDropDown = Driver.FindElement(By.XPath(xpathidofthecountrydropdown));
countryDropDown .Click();
//Driver.FindElement(By.XPath(xpathidofthecountrydropdown)).Click;
var selectElement = new SelectElement(countryDropDown);
selectElement.SelectByText(country);
}
I am able to call this function and this is executing successfully without any error messages. I am not able to select the expected keyword eventhough it exists.
Currently I am having a workaround of Clicking on the same id twice and that makes the code to work. With the commented section uncommented But I dont think that is correct workaround. Let me know your thoughts on this.
Thanks
Yah, it doesn't work well with Firefox. I had to use this as a workaround using jQuery. Feel free to modify this code with regular JavaScript if you don't have jQuery on the page.
public static void SetDropdownSelectedOptionByText(IWebDriver driver, string tagId, string newText, int sleepTime)
{
// not working when selecting client id of certain types of ASP.NET user controls
//new SelectElement(driver.FindElement(By.Id(tagId))).SelectByText(newText);
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("$('#" + tagId + " option:contains(" + Element.NonNullValue(newText) + ")').attr('selected', 'selected')");
js.ExecuteScript("$('#" + tagId + "').change()");
System.Threading.Thread.Sleep(sleepTime); // wait for dependent dropdown to load its values
}
Normally the select class handles the selection without you even clicking the drop down. It should work both in FF and Chrome, Ie has some other issues with Select. Try not to click the drop-down button at all. If Select class does not function the way it suppose to try clicking and navigating options sending up down keys and then pressing enter.