cssSelector 中包含的替代方案?Selenium 网络驱动程序 [英] Alternative of contains in cssSelector ? Selenium WebDriver
问题描述
我正在使用 selenium 2 (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')"));
或
driver.findElement(By.cssSelector("css=.gwt-Button:contains('Run Query')"))
谁的html是这样的:
whose html is like :
<button type="button" class="gwt-Button" id="ext-gen362">Run Query</
button>
由于 id 是动态生成的,我无法使用该 ID.
As the id is dynamically generated, I can't make use of the ID.
有没有办法使用 cssSelector 和 contains 之类的东西?这可能吗?
Is there any way to use cssSelector with something like contains ? Is this possible?
推荐答案
你不能用 CSS 选择器做到这一点,因为 CSS 中没有 :contains()
这样的东西.这是一个多年前被放弃的提议.
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.
如果您想通过元素文本进行选择,您将使用 XPath 选择器.类似的东西
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']"))
driver.findelement(By.xpath("//button[contains(., 'Run Query']"))
或
driver.findelement(By.xpath("//[contains(concat('', @class, ''), '.gwt-Button') and contains(., 'Run Query']"))
driver.findelement(By.xpath("//[contains(concat(' ', @class, ' '), ' .gwt-Button ') and contains(., 'Run Query']"))
这篇关于cssSelector 中包含的替代方案?Selenium 网络驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!