Selenium:有没有办法在整个页面中搜索给定的关键字?

May*_*aya 1 c# search selenium

我正在测试一个与Google非常相似的网站.因此,如果用户输入了一些搜索字词,例如"Selenium Tutorial",则结果页面会有一堆带有片段的链接.

每个结果都在一个表格中.因此,如果有10个结果,则有10个表.对于每个表(结果),有几行(比如r1,r2,r3等).r1是结果的标题,r2是大小,格式,作者等的描述,r3是片段.我需要确保r3包含"selenium""教程"这个词.我尝试过这样的事情:

String variable2 = Sel.GetTable("//div[@id='RepeaterContent']/table[2].3.1");
Run Code Online (Sandbox Code Playgroud)

这给了我r3中的文字,片段.但是,我需要获取所有表格(结果)的文本(片段).然后使用像contains这样的内容在片段上执行搜索.

我不确定以下代码是否有用,因为我无法复制源代码,但结果页面的源代码如下所示:

<HTML>
<BODY>
<DIV id="RepeaterContent">
<TABLE>
<TR>
  <TD>
    <SPAN>
      <a id="linkcft87021" class="ResultList_Title_Link" title="" href="DocsDetail.aspx?searchtranid=1bnt5d92" target="">Sample Tutorial</a>
      <br>
    </SPAN>
   </TD>
</TR>

<TR>
  <TD>
    ...an integrated development environment for performing Selenium tests...
  </TD>
</TR>
</TABLE>

<TABLE>
<TR>
  <TD>
    <SPAN>
      <a id="linkcft87021" class="ResultList_Title_Link" title="" href="DocsDetail.aspx?searchtranid=1g5t5d92" target="">Selenium</a>
      <br>
    </SPAN>
   </TD>
</TR>

<TR>
  <TD>
    ...With Selenium you can automate the whole process and run the test as required. In this part we will see how to create a simple test in Selenium... 
  </TD>
</TR>
</TABLE> 

</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)

以下是我的硒测试:

namespace Automation
{
    [TestClass]
    public class SEARCH
    {
        public ISelenium Sel;
        public string TestStatus = "FAIL";


        // Constructor
        public LIT_SEARCH_TAB()
        {
            Sel = new DefaultSelenium("localhost", 4444, "*iexplore", "http://localhost:8080");
      Sel.Start();
        }


        [TestMethod]
        public void SEARCH_METHOD()
        {
            Sel.Open("http://localhost:8080");
            Sel.Type("Search_Text_Box", "Selenium Tutorial");
            Sel.Click("Go");

         if (Sel.IsTextPresent("Selenium") || (Sel.IsTextPresent("Tutorial"))) 
         {
                   TestStatus = "PASS";
         }

       }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我想确保在整个结果页面中有"Selenium"和"Tutorial"这两个词.在Selenium有没有办法满足我的要求?有些方法可以在整个页面中搜索这些关键字,并验证关键字是否存在?提前致谢.

Tne*_*nem 6

您可以IsTextPresent(pattern)用来检查页面上是否有特定的字符串.pattern可以是其中之一glob,regexp或者exact默认是glob.