简体   繁体   English

CSS选择器路径-TestComplete Javascript无法单击按钮

[英]CSS Selector Path - TestComplete Javascript Can't Click the Button

I have this piece of HTML code 我有这段HT​​ML代码

在此处输入图片说明

I am using Testcomplete (Javascript) to target the button and try to click it. 我正在使用Testcomplete(Javascript)定位按钮并尝试单击它。 I think it is more about my CSS path/Javascript rather than TestComplete itself. 我认为这更多是关于我的CSS路径/ Javascript,而不是TestComplete本身。

I tried this code and it works fine 我尝试了这段代码,它工作正常

var path2 = "#Table > div > table > tfoot > tr > th:last-child > span > div";
var z = Page.QuerySelector(path2);
z.Click();   // Works Fine

I also tried this one and it works fine 我也尝试过这个并且效果很好

var path2 = "#Table > div > table > tfoot > tr > th:nth-child(7) > span > div";
var z = Page.QuerySelector(path2);
z.Click();  //Works Fine

But when I tried this one, it won't find and click the button 但是当我尝试这个时,它不会找到并单击按钮

var path2 = "#Table > div > table > tfoot > tr";   // path to parent table
var z = Page.QuerySelector(path2);
var y = z.QuerySelector('div.blue'); // Look for the child from that parent with tag div and class blue
Log.message(y.getAttribute("class"));  // Will return "button blue w-icon footer-add"
y.Click();  // Will return Javascript Runtime Error. y.Click is not a function

I don't know why if I find the parent node first and then search for it's child , Javascript won't be able to find it and click it ? 我不知道为什么如果我先找到父节点然后搜索它的子节点,Javascript将无法找到它并单击它?

The reason for this behavior can be explained by the following text from the QuerySelector Method (Page Objects) TestComplete help topic. QuerySelector方法(页面对象) TestComplete帮助主题中的以下文本可以解释这种现象的原因。

Result Value 结果值

If a TestComplete web object matches the specified selector, then the method returns this object. 如果TestComplete Web对象与指定的选择器匹配,则该方法返回此对象。

If there is no matching TestComplete test object, the method returns the appropriate HTML object. 如果没有匹配的TestComplete测试对象,则该方法返回适当的HTML对象。

In your case, the first found object ( var z = Page.QuerySelector(path2); ) does not have a corresponding TestComplete object, so the native HTML object is returned. 在您的情况下,第一个找到的对象( var z = Page.QuerySelector(path2); )没有相应的TestComplete对象,因此将返回本机HTML对象。 When you query the second object ( var y = z.QuerySelector('div.blue'); ), you actually calls the native QuerySelector method and not the TestComplete one that can return a TestComplete object. 当查询第二个对象( var y = z.QuerySelector('div.blue'); )时,实际上是调用本机QuerySelector方法,而不是调用可以返回TestComplete对象的TestComplete方法。 So, you get the native object as a result of the second query as well even while there is a corresponding TestComplete object. 因此,即使存在相应的TestComplete对象,您也将通过第二次查询获得本机对象。 The Click method is appended to TestComplete objects and you cannot use it when working with native HTML objects. Click方法将附加到TestComplete对象,并且在使用本机HTML对象时不能使用它。

So, the only option in your case to break the long path into two pieces is seems to be this one: 因此,在您的情况下,将长途分成两部分的唯一选择似乎是:

  var path2 = "#Table > div > table";
  var z = Page.QuerySelector(path2);
  var y = z.QuerySelector('tfoot > tr > th:last-child > span > div.blue');
  Log.message(y.getAttribute("class"));
  y.Click();

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

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