C# 当我使用Xpath在Selenium C中搜索元素时,我的测试总是失败#
我试图检查页面上的文本,当我检查它时,我看到:C# 当我使用Xpath在Selenium C中搜索元素时,我的测试总是失败#,c#,selenium,xpath,selenium-chromedriver,C#,Selenium,Xpath,Selenium Chromedriver,我试图检查页面上的文本,当我检查它时,我看到: < div class="welcome-message"> < h1 data-bind="text: $.t('wfo.Common:wfo.Welcome'), attr: { title: $.t('wfo.Common:wfo.Welcome') }" title="Welcome">Welcome</h1> public static bool IsAt { get
< div class="welcome-message">
< h1 data-bind="text: $.t('wfo.Common:wfo.Welcome'), attr: { title: $.t('wfo.Common:wfo.Welcome') }" title="Welcome">Welcome</h1>
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
通过实现以下代码:
public static bool IsAt {
get{
var h3 = Driver.instance.FindElements(By.XPath("//div[@class='welcome-message']/h1[@title='Welcome']"));
if (h3.Count >0)
return true;
return false;
}
}
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
但是当我运行这个案例时,我得到了失败:看到了“notin Admin Page”错误消息
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
我试图做的是根据搜索条件返回一个IwebElements列表,并且在找到元素时应该返回一个true响应,直到现在,我假设我想要匹配“true”条件。我是硒行业的新手,非常感谢您在这方面的帮助。我将了解以下几点:
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
//div[@class=“'welcome-message']/h1[包含(@title='welcome')
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
By.ClassName('welcome-message');
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
IWebElement parent=Driver.FindElement(By.CssSelector(“div[class='welcome-message']);
IWebElement child=parent.FindElement(By.XPath(“.//h1[@title='Welcome]”);
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
public static bool IsAt {
get{
var h3 = Driver.instance.FindElements(By.CSSSELECTOR("div.welcome-message>h1[title='Welcome']"));
if (h3.Count >0)
return true;
return false;
}
}
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
我不确定..但我认为发生此错误是因为您的方法“IsAt”是静态的,请删除此修饰符并重试非常感谢@cruisepandey。我使用了此方法并检查了它:
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
当我替换你的
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
“如果”条件(h3.Count>0)为(h3!=null)
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
我也得到了预期的结果
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}
“FindElements”应该返回一个与搜索条件匹配的项目列表,我不知道为什么会出现if(h3.Count>0)在这种情况下不起作用吗?你有HTML吗?发送建议请在问题后面留下评论。非常感谢@cruisepandey。我使用了它并进行了检查:public static bool IsAt{get{var h3=Driver.instance.FindElements(By.XPath(“/*[contains(text(),'Welcome')]”);if(h3!=null)返回true;返回false;}}
public static bool IsAt
{ get
{
var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
if (h3!=null)
return true;
return false;
}