Nic*_*ahn 7 webdriver css-selectors selenium-webdriver
我正在使用selenium 2(WebDriver).
我找到一个按钮并按脚本单击:
driver.findElement(By.cssSelector("button:contains('Run Query')"));
Run Code Online (Sandbox Code Playgroud)
要么
driver.findElement(By.cssSelector("css=.gwt-Button:contains('Run Query')"))
Run Code Online (Sandbox Code Playgroud)
其html如下:
<button type="button" class="gwt-Button" id="ext-gen362">Run Query</
button>
Run Code Online (Sandbox Code Playgroud)
由于id是动态生成的,我无法使用ID.
有没有办法使用像contains这样的东西cssSelector?这可能吗?
Ros*_*son 12
你不能用CSS选择器来做这件事,因为:contains()在CSS中没有这样的东西.这是几年前被废弃的提案.
如果要按元素文本进行选择,则必须使用XPath选择器.就像是
driver.findelement(By.xpath("// button [contains(.,'Run Query']"))
要么
driver.findelement(By.xpath("// [contains(concat('',@ class,''),'.gwt-Button')并包含(.,'Run Query']"))
仅靠 CSS 无法满足您的需求;您无法按文本进行过滤。您可以使用 js 来获取元素的 id,或者循环遍历代码中的所有按钮,直到找到具有正确文本的按钮。如果这是在 python 中:
[btn for btn in browser.find_elements_by_css_selector('button')
if 'Run Query' in btn.text]
Run Code Online (Sandbox Code Playgroud)
您也可以轻松地概括这一点并创建一个辅助函数。
| 归档时间: |
|
| 查看次数: |
35787 次 |
| 最近记录: |