cssSelector 中包含的替代方案?Selenium 网络驱动程序 [英] Alternative of contains in cssSelector ? Selenium WebDriver

查看:25
本文介绍了cssSelector 中包含的替代方案?Selenium 网络驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆