在cssSelector中包含的替代方法? Selenium WebDriver [英] Alternative of contains in cssSelector ? Selenium WebDriver
问题描述
我使用的是selenium 2(WebDriver)。
我找到一个按钮并点击脚本:
driver.findElement(By.cssSelector(button:contains('Run Query')));
或
pre>其html如下:
< button type =buttonclass =gwt-Buttonid =ext-gen362>运行查询< /
button>
由于id是动态生成的,因此我无法使用ID。
有什么办法使用cssSelector与类似contains的方法吗?这是可能的吗?
你不能使用CSS选择器,因为没有这样的事情:contains()
。这是一个在几年前放弃的提案。
如果你想通过元素文本选择,你将使用一个XPath选择器。
driver.findelement(By.xpath(// button [contains(。,'Run Query' / p>
或
driver.findelement .xpath(// [contains(concat('',@class,''),'.gwt-Button')and contains(。,'Run Query']))
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?
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']"))
这篇关于在cssSelector中包含的替代方法? Selenium WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!