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.
Related
I am trying to automate the scheduling of pinning items to Pinterest using Tailwindapp.com. I am using a console app in .NET (C#) with Selenium Chromedriver. I start up the browser and enable the tailwind extension and login to tailwind. Then I go to the site I am trying to pin images from, get to the product page, search for the button and attempt to click it. That's where it falls apart. The 'Schedule' button in Tailwind appears over all images on the page as you hover. When I do an XPath search, it only returns a single button for the whole page (the console line below shows 1).
public static void ClickScheduleButton(IWebDriver driver) {
// get all the buttons and then use the first one
IList<IWebElement> buttons = driver.FindElements(By.XPath("//*[#id='tw_schedule_btn']"));
Console.WriteLine("Number of items found: " + buttons.Count());
IWebElement scheduleButton = buttons.ElementAt(0);
Actions actions = new Actions(driver);
actions.MoveToElement(scheduleButton).Click().Perform();
}
On the perform method, I get the following error: OpenQA.Selenium.WebDriverException: 'javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
From what I've read, this seems to mean that there are more than 1 element but I seem to have ruled this out with the number of buttons found. I have attempted to do a wait to make sure it's available but I do not believe that is the issue.
I have tried to find an example just trying to do this with a Pinterest button because in theory it would be the same logic but I cannot find anything for that either.
My assumption is that it's a problem just getting the button to appear on the correct image? But that's just a guess.
If I'm understanding this correctly you want to click the button who appears in when you hoover the mouse in the image.
If you can hoover the mouse and wait until the expected xpath appears should solve it.
protected virtual void HooverMouse(By element)
{
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(element)).Build().Perform();
}
The element will be the first image you have, so you must change your driver.findelements("the button") to the "images" and loop arround this.
In pseudo code will be:
Hoover(image) -> WaitButtonAppears -> ClickButton
The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click
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.
Unable to click and select the value from dynamic drop down. Please find the below piece of code -
public static void main(String[] args)
{
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
//WebDriver Driver = new ChromeDriver();
WebDriver Driver = new FirefoxDriver();
Driver.get("http://www.spicejet.com/");
Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS)
Driver.findElement(By.xpath(".//*[#id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
}
Also I notice that Eclipse keeps on running after opening the Spicejet.com and it does not click on any drop down. To stop the execution I need to click manually on the Terminate button else it will not stop and go on for long time (4-6 hrs I believe)
You can use following code to select any value, In this code, I selected Goa (GOI). For more information, it is not a dropdown. It is a web table.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.spicejet.com/");
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXTaction")).click();
driver.findElement(By.xpath("//div[#id='dropdownGroup1']/div/ul[2]/li[4]/a")).click();
When I open that website, it sometimes seems to wait for the user to choose mobile or desktop site. Is that why it's hanging?
If I click past that (either with Selenium code or manually):
x1path = "//a[#class='desktop-view-button']"
WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
driver.find_element_by_xpath(x1path).click()
this code opens the menu:
x1path = '//*[#id="ctl00_mainContent_ddl_originStation1_CTXTaction"]'
WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
driver.find_element_by_xpath(x1path).click()
But then you have to choose which dropdown element you want (I don't think your code does that.)
x1path = '//div[#id="dropdownGroup1"]/div/ul/li[6]/a'
WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
driver.find_element_by_xpath(x1path).click()
The ul/li[6] selects the 6th element in the first column (Belagavi).
I'm trying to build my first test with selenium and got a problem.
I'm searching for a element, no problem. I can click on it, get the
text in the element... every thing works fine.
But double click on the element just doesn't work. Selenium
clicks in the wrong location. I made a screenshot of this situation:
Screenshot
To find the row i use xpath and search for the text in the cell, but this text is unique(I checked it)
private readonly string _identityPath = ".//td[.= 'All Employees']";
...
mainPage.FindElement(By.XPath(_identityPath)).Click(); //Works(dotted box)
Actions builder = new Actions(mainPage);
IAction doubleClick = builder.DoubleClick(mainPage.FindElement(By.XPath(_identityPath))).Build();
doubleClick.Perform(); //wrong location/element
/*
Actions action = new Actions(mainPage);
action.DoubleClick(mainPage.FindElement(By.XPath(_identityPath)));
action.Perform(); *///wrong location/element
This page is in an iframe and the grid is a dojo component... maybe the problem
comes from there. Any ideas whats wrong? I have no idea where this is coming from. :/
Greets
It is common issue that Actions builder does not work.
Using JavaScript should help - find answer in this thread:
Selenium 2/Webdriver - how to double click a table row (which opens a new window)
If the element is in an iframe, you need to switch to that iframe in order to interact with the element.