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).
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
I try to click on the login button(Đăng nhập) to show up the login box, but fail to achieve it.
The loginbox just doesn't show up.
Selenium, webdriver are all latest version
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
// driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
// driver.FindElement(By.XPath("//a[#href='#loginform']//span")).Click();
// driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']//span"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_username")));
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_password")));
// var loginBox= wait.Until(ElementIsClickable(By.Id("loginform"))); >> fail
driver.Scripts().ExecuteScript("document.getElementById('navbar_username').style.display='inline';");
driver.Scripts().ExecuteScript("document.getElementById('navbar_password').style.display='inline';");
Console.ReadKey();
}
C# extension:
public static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
return (IJavaScriptExecutor)driver;
}
There are 2 problems.
1- There's a webpage coming up prior to the actual forum page, when you are navigating to the site. Below is the image for that:
So you have to click on the button, that is highlighted above first. And, then after you will be able to navigate to the forum's page.
2- Your button is certainly getting clicked, but since the webpage has not properly loaded, the click action is not proceeding.
Hence, you need to wait for certain element that gets loaded when the page is loaded properly.
Below code will help you out:-
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); //Give the implicit wait time
driver.FindElement(By.XPath("//button[#id='btnSubmit1']")).Click();// Clicking on the button present in prior page of forum
//Waiting till the element that marks the page is loaded properly, is visible
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ExpectedConditions.ElementIsVisible(By.XPath("//*[#id='vtlai_topx']/a")));
driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
...
You can proceed with rest then.
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.
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.
From the WatiN website:
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
The above sample works just fine. However, since I do not want to open up a browser window, I modified the above code to use MsHtmlBrowser:
// goto the google website.
var ie = new MsHtmlBrowser();
ie.GoTo("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
The TypeText line is throwing an exception. Any idea what's wrong?
The MsHtmlBrowser is only there to find elements and read their attribute values. There is no support for clicking a link, typing text, firing events, no session state or any other way of interact like with a normal browser. So us it for scrapping only.
HTH,
Jeroen