Testing for CSS Selenium locators from the browser - selenium

In general, when I want to test the validity of a locator to be used in Selenium, I test it using the Firebug console.
i.e., I write: $$("a#someLink") in the Firebug console and the corresponding link becomes highlighted in Firefox.
However, if I test in Firebug for a locator like:
table#someTable tr:nth-of-type(2) td:nth-of-type(2)
Firebug doesn't show anything... Even though the locator works fine from Selenium...
I guess Selenium uses some 'hacks' for CSS locators, which Firebug does not understand...
Is there any way around it? Would using Xpath locators allow me to test for those kinds of locators?
Thank you very much

Firebug has an extension called FireFinder, which allows you to test xpath or css locators and it shows all the matches on the page. Here's a link.

Related

finding elements in edge and IE with selenium java based

i try to find elements in edge or IE and it look like that these two browsers are not working with Xpath, more over, i tried to launce my automation in parallel with cheome, IE, Edge, and in Xpath its not working at all (just in chrome it works) but when i change it to css.. it start working
second thing that i noticed that at the CTRL+F while looking for elements, its not highlighted like in chrome
so is there any way to work with xpath? also, it can be that elements in chrome are not the same in edge or IE with css for example? and if so, how can i run in parallel test with that browsers?
This xpath should work in all browsers:
"//foo[contains(#bar,'xy')]"
"//a[contains(#href,'xy')]"

nightwatch xpath selector not working

I'm currently working on a node/express.js app for which I'm writing some e2e tests in nightwatch. Today I hit a roadblock while trying to search for an element using the XPath locator strategy. Basically I can search for elements using any of the following:
//div[#data-pino-name='userIdSection']
//input[#name="password"]
//input[#name="username"]
//button[#data-pino-name="submit"]
//a[#data-pino-name="cancel"]
By the way, all of the selectors above work fine using the chrome tools.
However, using the following:
//pre[#data-pino-name="requiredErrorMessage"]
doesn't work at all. I'm surprised since I expected the <pre> tag to be treated the same as any other html tag. However, the test returns a "element was not found" for all elements with the pre tag.
Anyone guidance would be appreciated.
You can follow this approach in writing XPath based on your scenario
//div[#data-pino-name='userIdSection']/pre
//div[#data-pino-name='userIdSection']/pre[#data-pino-name="requiredErrorMessage"]

Is it possible to inject html into my selenium tests?

So lets say we have WebElement foo. foo has a crazy looking, consistently changing xpath. Hardcoding foo gets really annoying when you have to constantly do so.
I just discovered the html edit on Firebug and it made me wonder, is there anyway I could just write in id values for use in my Selenium tests? The distinct lack of no id values in any of the WebElements under test is getting real old.
Thanks in advance.
Editing the HTML in Firebug or Chrome devtools is just transient. As soon as you close the browser, your changes go away. There is no real solution to what you are asking other than to request that your devs add IDs to the desired elements. I'm assuming you don't own the site or that's not possible or you would have mentioned it. Welcome to test automation where the site creators don't take test automation into account when creating a site... :)
For the question, Is it possible to inject html into my selenium tests, the answer is yes. You can use JavaScriptExecutor to inject html into your WebPage.
((JavascriptExecutor) driver).executeScript("arguments[0].id = 'abc';", element);
Just as JeffC mentioned, for injecting the id or any HTML, you would have to identify the required element first. Also, the changes will be lost after you refresh the page.

Selenium css selectors on element with huge list of classes

I've been working on automation of a product that uses Dojo. The html I'm working with is very messy.. I need to click on div that has following css selector
div.dijit.dijitReset.dijitInline.dijitLeft.dijitTextBox.dijitComboBox.dijitDateTextBox.dijitValidationTextBox.dijitTextBoxError.dijitComboBoxError.dijitDateTextBoxError.dijitValidationTextBoxError.dijitError
I'm using firefinder plugin in Firefox and it can see the element all the time, out of 2 chrome plugins I have (CSS selector tester and CSS and Xpath checker) only the first one can find the element.
When I run my selenium code I get org.openqa.selenium.NoSuchElementException.
I tried selecting classes with . and with [class=".."] as well and both failed.
Is there some selenium limitation on how many classes you can have assigned to your element before it stops seeing an element? What stable approach can I use to make my tests work?
Use FirePath plugin in firefox and look for unique classes so you only have 1 unique selector. Also look up CSS selectors, they will help you in the long run
http://www.w3schools.com/cssref/css_selectors.asp

Selenium WebDriver Issue with cssSelector

I am trying to click on a button with using a CSS selector in Selenium 2.0 WebDriver. The issue is the script which I am able to run with Selenium RC is not working with WebDriver.
Code:
Selenium RC:
selenium.click("css=.gwt-Button:contains('Run Query')");
which works absolutely fine.
Selenium WebDriver:
driver.findElement(By.cssSelector(".gwt-Button:contains('Run Query')")).click();
which does not work.
I am using: selenium-server-standalone-2.9.0.jar with Firefox version 5.0. Could anyone help me in figuring out why the cssSelector is not working with WebDriver?
'Contains' is being deprecated from CSS 3. Webdriver supports whatever natively supported by the browser. It works in selenium RC because RC uses Sizzle library for css selectors and it supports 'contains'. Did you try something like,
WebElement element = driver.findElement(By.cssSelector(".gwt-Button[type='button']");
element.click();
If this is not unique then perhaps you might need to filter it further down. If your site uses jQuery then you could use 'contains' selector from jQuery.
JavascriptExecutor js = ((JavascriptExecutor)driver);
WebElement element = (WebElement) js.executeScript("return $(\".gwt-Button:contains('Run Query')\")[0];");
element.click();
that syntax looks valid and when I dropped some elements on a test page and then went to my console and did $(".gwt-Button:contains('Run Query')").length I got a 1 when I changed the text of the buttons. So the only thing I can think of is maybe you have a Run Query button that exists on the page but isn't displayed and so you get the runtime exception. You might want to do a check to see if it's displayed before clicking on it. Or creating a list of the elements and then removing all hidden (display == false) elements.