Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
497 views
Welcome To Ask or Share your Answers For Others

1 Answer

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']"))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...