how to locate element with selenium webdriver - selenium

After exporting a test in Visual Studio using Selenium webdriver, I execute the test case in VS and it fails when trying to locate an element in the test. The element is a tab within a page but the test case cannot find the id of the tab. This is the failing line driver.FindElement(By.Id("ui-id-14")).Click(); and I also tried to locate the element with the ClassName driver.FindElement(By.ClassName("ui-tabs-anchor")).Click(); by inspecting the element in Chrome browser but it still fails. Anyone can tell me what am I doing wrong here? Thank you in advance.
driver.FindElement(By.Id("ui-id-14")).Click();
driver.FindElement(By.ClassName("ui-tabs-anchor")).Click();

How Do you initalize you driver? A common mistake is not having a implicit wait with selenium. The driver tries to locate the element as soon as the document is ready, which is not feasible with "normal" JS applications as they are rendered after the document is ready. Try:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
This way the driver will try to find the element every 0.5 seconds up to 30 seconds before throwing an Exception. By the way: Welcome to stackoverflow. Please consider posting your code/code snippets, that makes analysis often easier. My suggestion is purely made from my experience with people being new to Selenium.

Related

issues in running selenium in internet explorer

Hi I am trying to run my selenium webdriver on IE9.
WebDriver version : 2.32.0
IE:9
IEDriverServer_win32:2.32.3
windows7
Below is my code:
File IEDriver=new File(System.getProperty("user.dir")+File.separator+"BrowserDrivers"+File.separator+"IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
DesiredCapabilities cap=DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver=new InternetExplorerDriver(cap);
driver.get("http://in00616:8421/GS");
Thread.sleep(3000);
//driver.findElement(By.id("j_username")).sendKeys("admin");
//driver.findElement(By.id("j_password")).sendKeys("admin");
driver.findElement(By.xpath(".//input[#id='j_username']")).sendKeys("admin");
driver.findElement(By.xpath(".//input[#id='j_password']")).sendKeys("admin");
driver.findElement(By.id("login")).submit();
Thread.sleep(2000);
driver.findElement(By.xpath(".//button[text()='Securities']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//span[text()='Issue']")).click();
Thread.sleep(2000);
driver.findElement(By.id("tabSecurities_Issue_Request_for_Issues")).click();
Above code logs in to my site but then when I try to click on Securities button I am not able to do it. Securities button starts flickering and then I am notified that unable to find the element.
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath == .//span[text()='Issue Type']
(WARNING: The server did not provide any stacktrace information) –
Same code works fine in FireFox.
Please help as i am suppose to test my UI on InternetExplorer.
I think it is the version compatibility issue.
Can anyone suggest the compatible version set for IEDriverServer, Selenium WebDriver and IE which is in working condition.
As this SO answer points out, IE does not have native XPath support. Instead, Selenium WebDriver uses an old third party xpath library when IE is being used. Firefox has integrated support for XPath, which is why your selectors work fine in that browser.
I would highly recommend you update your selectors to instead use CSS selectors. They are supported across all browser, are easier to read, understand, and pick up, and they are pretty fast.
You can learn more about how to use CSS selectors from some different tuturials here, here, and here, and a CSS selectors cheatsheet.
Also, whenever possible, please try to not select an element by the text it contains. If you can select an element by its ID, class, other attribute, or even through the DOM chain (i.e. "div.1 > div.2 > span.a > a.b"), is better than trying to select an element by text.
Webdriver has difficulty with IE using locators. It seems like Murnal has difficulty using CSS locator. My advice would be you HAVE to use other locators if one doesnt work. This issue comes again and again while using non firefox browser. In the meantime an easier way to come up with alternate locator is use Firefox selenium IDE, there in the boxes where you type command you will see it gives alternate locator as well. Copy that and try plugging tha in your webdriver's findelement script.
Hi all i have found out that it was the issue of Selenium Webdriver 2.32 with IEDriver_Server2_32. After trying out permutation & Combination with latest available webdriver versions and IEDriver_Server, i have found out suitable stable configuration to work on IE9 below is the stable configuration : Webdriver : 2.33.0 IEDriver_Server : 2.33.0. There is still small issue but i am trying to look for workaround. Issue : In IE if some control's tooltip overlaps other control than IE is not able to find out that control. i guess this issue is with IEs working. IE uses nativeEvents to perform operation hence it is not able to locate that control. In FF it is able to find out that control and it is working fine. Thanks everyone.

Selenium: Can't SendKeys() to an item that was below the visible window but was made visible by Click()

I have this problem with a text field that is visible at the time of the SendKeys. I'm using IEDriverServer.exe and C#.
Here's how I can reproduce the problem:
The text field in question is visible in the window but you have to scroll down to see it. To scroll down I click on the element using code like this:
var element = driver.FindElement(By.Xpath("…"));
element.Click();
This scrolls the window down and makes the text field visible.
But when I try to send text to now-visible window:
element.SendKeys("blah");
I get the exception:
When_applicant_enters_application.Should_be_instantly_approved_on_external threw exception: OpenQA.Selenium.ElementNotVisibleException: Element is not displayed
How can I fix or workaround this problem?
Selenium version: 2.32.1
OS: Windows 7
Browser: IE
Browser version: 9.0.15
I've written code demonstrating the problem and submitted it to the Selenium tech support volunteers.
The full discussion is at http://code.google.com/p/selenium/issues/detail?id=5620
but the take-home is:
// Doesn't work
// driver = new InternetExplorerDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
// driver = new FirefoxDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(#"http://localhost:8080/Bug5620/"); // Hosted on Tomcat
so there may be a problem that possibly involves IE, IE security settings, Visual Studio local servers and/or the IE Driver. This may not even be a code problem, but something that needs to be documented, since other people are apparently running into the problem.
I don't know where the problem is exactly but I do have a work-around at this point, which is to use the Firefox Driver.
Thanks for your help, Jim. If you find out a better way of dealing with the problem, please add an answer here for the other folks.

Selenium Webdriver hover not working

Selenium Webdriver 2.31.0
with Scala 2.9
Anyone know how to do a mouse hover in Firefox? I'm basically trying to hover over an element to display a tooltip.
This code fails to move the mouse over the element specified.
val webElement = webDriver.findElement(By.cssSelector(myElement.queryString))
val builder = new Actions(webDriver)
val hover = builder.moveToElement(webElement).build()
hover.perform()
I have also tried mouse events without success (as described here WebDriver mouseOver is not working properly with selenium grid)
This is somewhat anecdotal since I don't have an exact technical explanation, but I've experienced this in the past and have remedied by upgrading Selenium.
The first thing I check is to make sure my selenium is up to date. This includes dependencies, standalone-server and browser drivers (though, in this case, not applicable as Firefox is included with Selenium).
Another possible (and more probable) cause, more directly related to Firefox, is Firefox itself. It's been my experience that a Firefox update can, from time to time, break some selenium functions, particularly hovers. I've found that either upgrading selenium, or if no update has been released, downgrading Firefox will solve the problem.
I wish I had more detailed information to give you, but I'm still learning the finer details of this situation myself. If nothing else, I hope this points you in the right direction.
Since you havn't said you got any errors,
After build().perform(), provide a wait method say, Thread.sleep() for certain amount of time, since there are posibilities where mousehover performed in fraction of seconds and it may not be possible to see the tooltip.
Makesure the locator is correct (because you may point to someother locator which doesn't show up any tooltip)
Makesure you firefox supports the mousehover functionality
The code might resemble as same as your's, but give it a try(JAVA),
Actions builder = new Actions(driver);
WebElement we = driver.findElement(locator);
Actions perf= builder.moveToElement(we).build();
perf.perform();
Thread.sleep(1000);
You can look out the link for your ref : #firefox issue
As your issue is in Firefox, you may need to enable Native Events with webdriver, specifically
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
I've had to do this to get drag and drop working in Firefox on Unix, although it worked with the same code on a Windows box.

Selenium Webdriver Error Unknown strategy class

I've been trying to use Selenium to test my application. I wrote the initial tests using the Selenium IDE but, when I converted the IDE tests to Selenium Webdriver tests I got errors for about half of my code! So I'm going through each of the errors trying to get the code to work.
The error I'm getting in Web Driver is
ERROR: Caught exception [Error: unknown strategy [class] for locator [class=x-tool-close]]
In Selenium IDE it had simply been Command Click and target class=x-tool-close.
Thanks,
It has been awhile since I posted this question so I cannot be sure if I am addressing what the problem was exactly, but with that said the following is what I use for the command I mentioned above.
driver.findElement(By.xpath("//img[contains(#class,'x-tool-close')]")).click();
The structure of this command is very basic. Since the id of my elements are dynamic I search by xpath. Inside the quotation marks we have said xpath. We are searching for an 'img' element whose class attribute contains 'x-tool-close'. Then we are executing the selenium webdriver command click upon that element.

Selenium RC cant find locator

I have the following locator:
css=tr:contains('First page') + tr table td:contains('Yes') input
I can find it on the page using Selenium IDE, but my Selenium RC test fails trying to find it.
What can be the matter?
Try Using Fire bug to locate the element and then using the same try running the code in
selenium RC.
Many times the selenium IDE reads the location in Short due to which it fails try the above method.