简体   繁体   English

为什么这两个元素定位器调用的行为不同?

[英]Why do these two element locator calls behave differently?

I am writing webdriver automation using JavaScript to test a web app.我正在使用 JavaScript 编写 webdriver 自动化来测试 web 应用程序。 I have code that looks like this:我的代码如下所示:

var selenium = require('selenium-webdriver');
By = selenium.By;
until = selenium.until;

driver = new selenium.Builder().
  withCapabilities(selenium.Capabilities.chrome()).
  build();

getItems = async function() {
  try {
    var itemsXpath = '[xpath expression]';
    var items = await driver.findElements(By.xpath(itemsXpath));
    console.log(items.length + ' items found');
    ...
  }
  catch(err) {
    console.log(err);
  }
}

This succeeds.这成功了。 However, if I replace the await line with:但是,如果我将await行替换为:

var items = await driver.wait(until.elementsLocated(By.xpath(itemsXpath)),60000);

it does not execute as expected.它没有按预期执行。 Neither the following console.log() , nor the one in the catch() block, are displayed.既不显示以下console.log() ,也不显示catch()块中的那个。

From reading the documentation , my impression was that if there were items on the page which match the XPath locator, either of these functions would work, and return the elements.通过阅读文档,我的印象是,如果页面上有与 XPath 定位器匹配的项目,则这些函数中的任何一个都可以工作,并返回元素。 However, if the elements were not on the page, the second formulation would wait for them to appear (up to 60000 ms, in this case).但是,如果元素不在页面上,则第二个公式将等待它们出现(在这种情况下最多 60000 毫秒)。

Note that the second way of writing this does not work, even if the items are already on the screen at the time getItems() is called.请注意,第二种编写方式不起作用,即使在调用getItems()时项目已经在屏幕上。 Also, the program is completing in less time than the timeout specified in driver.wait() .此外,程序完成的时间少于driver.wait()中指定的超时时间。

Is there something I have misunderstood about how this works, such that this behavior actually makes sense?我对它的工作原理有什么误解,以至于这种行为实际上是有意义的吗? If so, can anyone clarify what is going on here?如果是这样,谁能澄清这里发生了什么?

The line you have written:你写的那行:

var items = await driver.findElements(By.xpath(itemsXpath));

will check for the element and returns Array<WebElement> if the given xpath matches with any element(s).如果给定的 xpath 与任何元素匹配,将检查元素并返回Array<WebElement>

But the wait methods what you used:但是您使用的等待方法:

var items = await driver.wait(until.elementsLocated(By.xpath(itemsXpath)),60000);

will not return any element.不会返回任何元素。 It simply waits until the element is located in the DOM.它只是等到元素位于 DOM 中。

Anyhow, you have specified 60 seconds to find the element using elementLocated method.无论如何,您已经指定了 60 秒来使用elementLocated方法查找元素。 This line will wait a maximum of 60 seconds to find an element matching with your xpath.此行将等待最多 60 秒来查找与您的 xpath 匹配的元素。 If the specified xpath matches with any element in less than 60 seconds, it will simply end that wait on that particular time and move to the next line.如果指定的 xpath 在 60 秒内与任何元素匹配,它将简单地结束该特定时间的等待并移至下一行。 Note that there won't be any error in such situations.请注意,在这种情况下不会有任何错误。 Hence, there is no chance of going to catch block.因此,没有机会去抓块。

You can refer to this page for more details on this.您可以参考此页面以获取更多详细信息。

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

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