Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

css selectors - 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?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...