我如何确定网站中是否有 Selenium 的特定文本

How can I decide is there a specific text in web site withSelenium

已编辑POST

首先,网站上有一个类似的问题,但它没有用,这就是我问的原因。 我应该决定 HTML 代码上是否有特定文本 LİSTELENECEK VERİ BULUNAMAMIŞTIR.,这里只有 id 属性是 HTML 代码:

<center id="genelUyariCenterTag">LİSTELENECEK VERİ BULUNAMAMIŞTIR.</center>

这是 C# 代码:

 WebClient client = new WebClient();
void goMadde15Down() {
                    driver.FindElement(By.LinkText("İŞVEREN")).Click();
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    driver.FindElement(By.LinkText("TEŞVİKLER VE TANIMLAR")).Click();
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    driver.FindElement(By.LinkText("4447/GEÇİCİ 15. MADDE LİSTELEME/SİLME")).Click();
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    string siteSource15 = client.DownloadString("https://uyg.sgk.gov.tr/IsverenSistemi");

                    string strText = driver.FindElement(By.XPath("//center[@id='genelUyariCenterTag' and contains(.,'BULUNAMAMIŞTIR')]")).Text;

                    if (strText.Contains("LİSTELENECEK VERİ BULUNAMAMIŞTIR."))
                    {
                        Console.WriteLine("madde15 there is no Excel");
                    }
                    else
                    {
                        Console.WriteLine("madde 15 there is an Excel");
                    }

问题是,条件总是收敛到 else 子句

引入WebDriverWaitElementExists并在xpath下使用。

您需要从 Manage Nuget Packages 安装 SeleniumExtras.WaitHelpers 包。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
string strText=wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath("//center[@id='genelUyariCenterTag' and contains(.,'BULUNAMAMIŞTIR')]"))).Text;
    if (strText.Contains("LİSTELENECEK VERİ BULUNAMAMIŞTIR."))
            {
                Console.WriteLine("madde15 there is no Excel");
            }
     else
            {
                Console.WriteLine("madde 15 there is an Excel");
             }

尝试更新一个没有 WebDriverWait

string strText=driver.FindElement(By.XPath("//center[@id='genelUyariCenterTag' and contains(.,'BULUNAMAMIŞTIR')]")).Text;

            if (strText.Contains("LİSTELENECEK VERİ BULUNAMAMIŞTIR."))
                {
                Console.WriteLine("madde15 there is no Excel");
                }
                else
                {
                Console.WriteLine("madde 15 there is an Excel");
                }

而不是 ifelse 如果您可以使用 try-catch 块,您可以使用以下代码:

 void goMadde15Down() {
       driver.FindElement(By.LinkText("İŞVEREN")).Click();
       driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
       driver.FindElement(By.LinkText("TEŞVİKLER VE TANIMLAR")).Click();
       driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
       driver.FindElement(By.LinkText("4447/GEÇİCİ 15. MADDE LİSTELEME/SİLME")).Click();
       driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

            try  {
                    Console.WriteLine("There is an Excel File");
                    //Download code here


                 }
            catch (Exception e)
                 {
                        Console.WriteLine("There is no Excel File");
                 }

此外,try catch 块比 ifelse

更有效