I am trying to write a UI test for an application that uses a kendo grid. I am using selenium to drive the browser, and I am having trouble selecting a row in the grid.
I have been able to select the correct row like this:
IWebElement matterToSelect = MatterToSelectGrid.FindElement(By.XPath("//td[text()='" + matterId + "']/ancestor::tr"));
I was hoping to simply call .click() the webElement, but that did not work. So I tried building an action:
new Actions(Driver).MoveToElement(matterToSelect).Click().Build().Perform();
also no joy. Okay what about selecting it with js?:
IJavaScriptExecutor js = Driver as IJavaScriptExecutor;
js.ExecuteScript("return $(\"tr[data-uid='" + id + "']\").trigger('click');");
still nothing, what about a more direct selection just to see if it will work?
js.ExecuteScript("return $(\"#sourceGrid > table:nth-child(1) > tbody:nth-child(3) > tr:nth-child(1) > td:nth-child(1)\").click();");
grrrr still no luck. So my next thought was perhaps the kendo grid has a select() method I can use? Well it seems yes, but from what I read I also need to re-bind the grid? All the exaples I can find look something like this:
$("#sourceGrid").data("kendoGrid").select()
but I don't want to re-bind the grid from my test, so I have not tried this. Also, I am not sure what should be in data(), as all the grids are set up using the Html helper method, and use the .dataSource method to bind to action methods. Has anyone one got any better suggestions?
okay, I did it in the end like this:
IJavaScriptExecutor js = Driver as IJavaScriptExecutor;
js.ExecuteScript(String.Format("return $('td:contains(\"{0}\")').parent().addClass('k-state-selected');", matterId));
Don't get me wrong, I am not a fan of XPath but this is easiest way I found to select the first row in a Kendo grid. I gave my grid the Id of 'ticketGrid'. I then used Chrome Dev tools to inspect the first row. You can then right click and select 'Copy XPath' and I got this:
//*[#id='ticketGrid']/table/tbody/tr[1]
Then in my specFlow tests I have the following:
var wait = new WebDriverWait(WebDriver, timeout: TimeSpan.FromSeconds(5));
var firstRow = wait.Until(w => w.FindElement(By.XPath(#"//*#id='ticketGrid']/table/tbody/tr[1]")));
firstRow.Click();
Hope this helps
Related
I am not able to click on 'Albums' in Facebook.
The HTML is Albums how to locate the element 'Albums' in selenium web driver.
I tried with using driver.findelement(By.xpath(span[#class="_3sz"]) showing error as element not found
And, the html looks the following:
<span class="_3sz">Albums</span>
If I am understanding your problem correctly then that xpath you mentioned returns more than one elements. Use a text based search which is more easier and specific.
driver.findelement(By.xpath("//*[.='Albums']").click();
And, here . is used to directly point to the parent element. Additional wait might be needed to wait for the element to interact. Also, I am assuming you are trying to click the element.
EDIT
Driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
Driver.Navigate().GoToUrl("http://www.facebook.com");
Driver.Manage().Window.Maximize();
Driver.FindElement(By.CssSelector("#email")).SendKeys("your email");
Driver.FindElement(By.CssSelector("#pass")).SendKeys("your pass");
Driver.FindElement(By.CssSelector("[type='submit'][value='Log In']")).Click();
Driver.FindElement(By.CssSelector(".fbxWelcomeBoxName")).Click();
Driver.FindElement(By.XPath("//*[.='Photos']")).Click();
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[.='Albums']")));
Driver.FindElement(By.XPath("//*[.='Albums']")).Click();
By albumname = By.XPath("//strong[.='2014']"); //this should be your album name. In my case it's 2014
wait.Until(ExpectedConditions.ElementExists(albumname));
Driver.FindElement(albumname).Click();
wait.Until(ExpectedConditions.ElementExists(By.CssSelector(".fbPhotoAlbumHeader.fbPhotoAlbumOptionsPresent [type='file']")));
Driver.FindElement(By.CssSelector(".fbPhotoAlbumHeader.fbPhotoAlbumOptionsPresent [type='file']")).SendKeys(#"D:\Users\Saifur\Desktop\FacebookPicture\150232_585410621540701_1836495431_a.jpg");
wait.Until(ExpectedConditions.ElementExists(By.CssSelector(".pvm.phl.footerBox.uiBoxWhite")));
Driver.SwitchTo().ActiveElement();
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("[name='postPhotosButton']")));
Driver.FindElement(By.CssSelector("[name='postPhotosButton']")).Click();
Notice mine is C#
It always best practice to follow this sequence while selecting elements.
1) ID
2) CSS
3) XPath (This will have some issues with different browsers specially IE)
In this case, considering this is no other span class with same name. It would have work like this "span._3sz". Simple and powerful.
I'm trying to navigate in site: http://startupnationbook.com/startup-map
I want to click on link of Startups, however I'm not able to locate the element.
I tried:
elem = driver.findElement(By.xpath("//a[contains(text(),'Startups')]"));
and
elem = driver.findElement(By.xpath("//*[#id='listtoggle' and contains(text(),'Startups')]"));
In both cases I get: "Unable to locate element" error
What is wrong in my expressions and how can I locate the element to perform a click.
The map is in an iframe, you're probably not telling Selenium to look in there. I would also look for the span tag, and since the id listtoggle is used many times (poor design, making it worthless), just look for contains Startups.
// Also should probably use a wait here, in case the page takes too long to load
chromeDriver.switchTo().frame(chromeDriver.findElement(By.tagName("iframe")));
WebElement elem = chromeDriver.findElement(By.xpath("//span[contains(text(),'Startups')]"));
You can do like below :
WebDriver driver = new FirefoxDriver();
driver.get("http://startupnationbook.com/");
driver.findElement(By.xpath(".//*[#id='main-nav']/li[5]/a")).click();
Hope this will work. I have tested.
Before posting this, I've thoroughly researched all possible syntax available for this and got no avail.
The closest I had was using this code where the dropdown actually appeared but didn't select my desired option:
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")));
driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")).sendKeys("Local Move");
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"))).click();
I spent a significant amount of time today figuring this one out but I really failed on this one big time.
WebElement dropDownListBox = driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"));
Select clickThis = new Select(dropDownListBox);
Thread.sleep(5000L);
clickThis.selectByValue("1078");
It seems I needed Thread.sleep to let the system display the options completely before selenium can find the option I want for it to choose from.
Thanks guys!
To select a value from drop down. You need to use Select class from web driver.
driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")).sendKeys("Local Move");
Instead of the above line use this
WebElement ele = driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"));
Select dropdown = new Select(ele);
dropdown.selectByVisibleText("Local Move");
// Can select the dropdown using `index` and `value`
dropdown.selectByValue("Local Move");
dropdown.selectByIndex("1234");
To select a value from dropdown you should find the dropdown element by id then by value in the dropdown.
Try this:
new Select(driver.findElement(By.id("Dropdownid"))).selectByVisibleText("Text name");
I have using selenium webdriver 2.33.0 and i have a requirement of sending data with the following characters inside the data ("(", "#")
When we tried to send these characters using sendkeys
"WebElement dat = driver.findElement(By.xpath("xpathexpression);
dat.sendkeys("select * from (?s, ?p, ?o)");
The following data gets displayed in the textarea as select * from ?s, ?p, ?o)
the open bracket is missing. I have used selenium actions class sendkeys and robot sendkeys also.
I am not able to fix the issue. Can someone help me on this?
Ugly but efficient workaround: replace the opening brackets ( with the key sequence shift + 9, as suggested by user2935099.
dat.sendKeys(Keys.chord(Keys.SHIFT, "9"));
Funnily, this seems to work regardless of your current keyboard layout (I use an Irish locale with a French layout).
I stumbled upon this one with version 2.40.0, and it is definitely a bug in Selenium. Using the following in the Firefox JavaScript console works flawlessly:
var box = getElementById('SearchBox');
box.setValue('AB (CDE FGH)');
The issue is resolved when using actions class
WebElement dat = driver.findElement(By.xpath("xpathexpression");
dat.click();
Actions data = new Actions(driver);
data.sendKeys(Keys.chord(Keys.CONTROL,"a"),Keys.DELETE);
data.perform();
query = query.replaceAll("\\(", Keys.chord(Keys.SHIFT,"9"));
query = query.replaceAll("\\#", Keys.chord(Keys.SHIFT,"3"));
query = query.replaceAll("\\-", Keys.SUBTRACT.toString());
query = query.replaceAll("\\}", Keys.chord(Keys.SHIFT,"]"));
data.sendKeys(query);
data.perform();
I tried entering the same thing with my selenium setup and it worked fine in both firefox and chrome. I am not not very sure about the reason but one thing that I can notice that you are using very old selenium version(2.33.0) currently 2.37.0 is available. May be you should try updating the selenium and t should work as expected.
Same problem here, using 2.40.0 my workaorund was using clipboard, not pretty but testsystems have not much use for a clipboard anyway...
public static void setTextByClipboard(WebElement element, String text) {
StringSelection selection = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
element.sendKeys(Keys.chord(Keys.CONTROL, "v"));
}
I am submitting search criteria through form to third party URL. I tried to give target="someName". But when i am trying to focus on new window using "sameName" and validating some text there, selenium unable to find that locator or focus on that window. retuning false. please help on this. its urgent.
I also tried storeAllWindowNames or Ids selenium commands, also not working its returning main window ID instead of new open window.
the idea is in the following. as you try to validate some text in a new window, I do not see any necessity to get window name as you are able to get the xpath or cssselector of the text to be validated using firepath (firebug addon in ffox) and using command
String txt = driver.findElement(By.xpath(//...blablbalba)).getText().trim();
//or
String txt = driver.findElement(cssSelecot(html>..blalblabla...)).getText().trim();
//validation
Assert.assertTrue(txt.equals("blablabla"));
you can actually validate it.
or if you want to validate something and selenium is not able to cope with it then I recommend you to use js (getText using js):
String cssSelector=....
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+cssSelector+"\");");
stringBuilder.append("return x.text().toString();") ;
String res= (String) js.executeScript(stringBuilder.toString());
Assert.assertTrue(res.trim().contains("blablabla....") );
Hope this works for you.
This is the solution:
Well, if you have no choice but use it. Here is one solution:
if you have a target with your own name like target="popup", then you only need open the target window before click on the link. Example:
[b]store "javascript{selenium.browserbot.getCurrentWindow().open('', 'popup')}", "myWindow"[/b]
If you have a target="_blank", you need change the target before open the window.
store "javascript{this.page().findElement('link=My link who open the popup with target blank').target='popup'}", "myVar"
store "javascript{selenium.browserbot.getCurrentWindow().open('', 'popup')}", "myWindow"
Then, you click on the link and selectWindow("popup"). That is.
Another solution is rewrite the entire A tag and change the href/target by a javascript window.open but this is a litle more work to do.