Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative of contains in cssSelector ? Selenium WebDriver

I am using selenium 2 (WebDriver).

I am locating a button and clicking by the script:

driver.findElement(By.cssSelector("button:contains('Run Query')")); 

or

driver.findElement(By.cssSelector("css=.gwt-Button:contains('Run Query')")) 

whose html is like :

<button type="button" class="gwt-Button" id="ext-gen362">Run Query</ 
button> 

As the id is dynamically generated, I can't make use of the ID.

Is there any way to use cssSelector with something like contains ? Is this possible?

like image 977
Nick Kahn Avatar asked Jan 30 '13 01:01

Nick Kahn


1 Answers

You can't do this with CSS selectors, because there is no such thing as :contains() in CSS. It was a proposal that was abandoned years ago.

If you want to select by the element text, you'll have use an XPath selector. Something like

driver.findelement(By.xpath("//button[contains(., 'Run Query']"))

or

driver.findelement(By.xpath("//[contains(concat(' ', @class, ' '), ' .gwt-Button ') and contains(., 'Run Query']"))

like image 121
Ross Patterson Avatar answered Sep 28 '22 01:09

Ross Patterson