简体   繁体   English

Selenium WebDriver问题与cssSelector

[英]Selenium WebDriver Issue with cssSelector

I am trying to click on a button with using a CSS selector in Selenium 2.0 WebDriver. 我试图在Selenium 2.0 WebDriver中使用CSS选择器单击一个按钮。 The issue is the script which I am able to run with Selenium RC is not working with WebDriver. 问题是我能够与Selenium RC一起运行的脚本不能与WebDriver一起使用。 Code: 码:

Selenium RC: Selenium RC:

selenium.click("css=.gwt-Button:contains('Run Query')");

which works absolutely fine. 哪个工作绝对没问题。

Selenium WebDriver: Selenium WebDriver:

driver.findElement(By.cssSelector(".gwt-Button:contains('Run Query')")).click(); 

which does not work. 这不起作用。 I am using: selenium-server-standalone-2.9.0.jar with Firefox version 5.0. 我正在使用:selenium-server-standalone-2.9.0.jar和Firefox 5.0版。 Could anyone help me in figuring out why the cssSelector is not working with WebDriver? 任何人都可以帮我弄清楚为什么cssSelector不能使用WebDriver?

'Contains' is being deprecated from CSS 3. Webdriver supports whatever natively supported by the browser. 'Contains'正在从CSS 3中弃用.Webdriver支持浏览器本机支持的任何内容。 It works in selenium RC because RC uses Sizzle library for css selectors and it supports 'contains'. 它适用于selenium RC,因为RC使用Sizzle库作为css选择器,它支持'contains'。 Did you try something like, 你尝试过类似的东西吗

WebElement element = driver.findElement(By.cssSelector(".gwt-Button[type='button']");
element.click();

If this is not unique then perhaps you might need to filter it further down. 如果这不是唯一的,那么您可能需要进一步过滤它。 If your site uses jQuery then you could use 'contains' selector from jQuery. 如果您的站点使用jQuery,那么您可以使用jQuery中的'contains'选择器。

JavascriptExecutor js = ((JavascriptExecutor)driver);
WebElement element = (WebElement) js.executeScript("return $(\".gwt-Button:contains('Run Query')\")[0];");
element.click();

that syntax looks valid and when I dropped some elements on a test page and then went to my console and did $(".gwt-Button:contains('Run Query')").length I got a 1 when I changed the text of the buttons. 该语法看起来有效,当我在测试页面上删除一些元素然后转到我的控制台并执行$(“。gwt-Button:contains('Run Query')”)。当我更改文本时,我得到1的按钮。 So the only thing I can think of is maybe you have a Run Query button that exists on the page but isn't displayed and so you get the runtime exception. 所以我唯一能想到的就是你可能有一个Run Query按钮存在于页面上但是没有显示,所以你得到了运行时异常。 You might want to do a check to see if it's displayed before clicking on it. 您可能希望在点击之前检查它是否显示。 Or creating a list of the elements and then removing all hidden (display == false) elements. 或者创建元素列表,然后删除所有隐藏(display == false)元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM