C# - webdriver:如何引用对象

C# - webdriver: How to refer to the object

我正在进行自动测试,我想检查带有铭文 "Good morning" 的警报的出现(当我断言时)。 我正在使用 selenium-webdriver 在 C# 中编写测试。 我应该如何找到对该对象的引用?

我正在使用:driver.FindElement(By. ......

元素:

<div class="alert alert-dismissable alert-info">
<button aria-hidden="true" class="close" data-dismiss="alert">×</button>
Good morning
</div>

您不能使用 css 选择器直接查询文本。参见 this answer。您可以做的是获取具有 class 的所有元素,然后遍历它们以查找文本。

var alerts = driver.FindElements(By.CssSelector("div.alert.alert-dismissable.alert-info"));
Assert.IsTrue(alerts.Any(element => element.Text.Contains("Good morning")));