C# 如何使用Selenium C验证一个页面上的文本,然后验证另一个页面上的文本是否相同?

C# 如何使用Selenium C验证一个页面上的文本,然后验证另一个页面上的文本是否相同?,c#,selenium,C#,Selenium,例如:登录到一个有用户未知文本的网页,然后验证它在另一个网页上是否有相同的文本。您可以选择以下内容: 使用like查找第一页的对象: 获取HTML正文的值 使用字符将其拆分为单独的字符串 对第二页执行步骤1-3 迭代从第一页提取的字符串,并在第二页的字符串中查找匹配项 对结果做你需要的 示例代码: driver.Url = "https://stackoverflow.com"; string firstPageText = driver.FindElement(By.XPath("//bod

例如:登录到一个有用户未知文本的网页,然后验证它在另一个网页上是否有相同的文本。

您可以选择以下内容:

使用like查找第一页的对象:

获取HTML正文的值 使用字符将其拆分为单独的字符串 对第二页执行步骤1-3 迭代从第一页提取的字符串,并在第二页的字符串中查找匹配项 对结果做你需要的 示例代码:

driver.Url = "https://stackoverflow.com";

string firstPageText = driver.FindElement(By.XPath("//body")).GetAttribute("innerText");
HashSet<string> firstPageLines = new HashSet<string>(firstPageText.Split('\n'));

driver.Url = "https://stackoverflow.com/questions/56366177/how-do-i-verify-text-on-one-page-then-validate-it-is-that-same-text-on-another-p";

string secondPageText = driver.FindElement(By.XPath("//body")).GetAttribute("innerText");
HashSet<string> secondPageLines = new HashSet<string>(secondPageText.Split('\n'));

foreach (string line in firstPageLines) {
    if (secondPageLines.Contains(line)) {
        Console.WriteLine("This line is present in both pages: " + line);
    }
}
示例输出:


您是要求比较整页文本还是仅比较一个元素?快速答案是从第一页获取文本并将其保存到变量中。转到第二页,获取文本并进行比较。这就是你要做的吗?到目前为止你做了什么?我希望在一页上打印文本,然后在另一页上验证相同的文本。提前谢谢!为了澄清,您想在一页上输入文本,然后验证该文本是否显示在第二页上?
driver.Url = "https://stackoverflow.com";

string firstPageText = driver.FindElement(By.XPath("//body")).GetAttribute("innerText");
HashSet<string> firstPageLines = new HashSet<string>(firstPageText.Split('\n'));

driver.Url = "https://stackoverflow.com/questions/56366177/how-do-i-verify-text-on-one-page-then-validate-it-is-that-same-text-on-another-p";

string secondPageText = driver.FindElement(By.XPath("//body")).GetAttribute("innerText");
HashSet<string> secondPageLines = new HashSet<string>(secondPageText.Split('\n'));

foreach (string line in firstPageLines) {
    if (secondPageLines.Contains(line)) {
        Console.WriteLine("This line is present in both pages: " + line);
    }
}