4

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?

3
  • can you show the element that you are trying to select Commented Oct 25, 2011 at 14:51
  • <button type="button" class="gwt-Button" id="ext-gen362">Run Query</button> But I can not use the ID as that is dynamically genaratetd. Commented Oct 25, 2011 at 15:00
  • @Swagatika: id is dynamically generated. Is full text of id generated dynamically? or is some last portion of id dynamically generated? here id="ext-gen362" . If ext-gen is fixed and only value (362) is being changed then xpath with contains or starts-with can be used Commented Aug 2, 2012 at 9:49

2 Answers 2

8

'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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. Yep I could make it work by injecting sizzle library.
If your application uses jQuery then perhaps you don't need to inject sizzle library and pollute the DOM. AFAIK, sizzle is used as jQuery's CSS selector engine. In that case the second solution I mentioned could work out fine.
Yeah, the issue is my application does not use JQuery, thats why I had to use Sizzle separately and make the contains() method work. Thanks a lot for your help.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.