首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定网站withSelenium中是否有特定的文本?

如何确定网站withSelenium中是否有特定的文本?
EN

Stack Overflow用户
提问于 2020-02-04 11:37:08
回答 2查看 97关注 0票数 0

编辑的帖子

首先,有一个类似的问题在现场,但它没有工作,这就是为什么我要问。我应该决定在HTML代码上是否有一个特定的文本LİSTELENECEK VERİ BULUNAMAMIŞTIR.,只有id属性在这里是HTML:

代码语言:javascript
复制
<center id="genelUyariCenterTag">LİSTELENECEK VERİ BULUNAMAMIŞTIR.</center>

以下是C#代码:

代码语言:javascript
复制
 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子句。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-04 14:35:24

ifelse不同,如果可以使用try-catch块,则可以使用以下代码:

代码语言:javascript
复制
 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更有效。

票数 1
EN

Stack Overflow用户

发布于 2020-02-04 12:53:08

归纳WebDriverWaitElementExists并在xpath下面使用。

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

代码语言:javascript
复制
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的更新版本

代码语言:javascript
复制
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");
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60056560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档