【问题标题】:Testing with Selenium in C# - Website which I need to test changes XPath在 C# 中使用 Selenium 进行测试 - 我需要测试更改 XPath 的网站
【发布时间】:2020-01-23 06:13:29
【问题描述】:

我需要用 c# 在 selenium 中创建一个测试自动化工具来测试我公司的网站。一切正常,除了一件事:每次我在网站上重新加载任务(如“创建新用户”)时,某些字段的 XPath 都会发生变化。名字字段作为示例不是"//*[@id=\"0FirstName\"]" anymore, the new xpath would be "//*[@id=\"2FirstName\"]",并且 selenium 不再像以前那样识别该字段 -> 程序停止工作

有没有办法让动态函数忽略邮件前的数字?

driver.FindElement(By.XPath("//*[@id=\"0FirstName\"]")).SendKeys("Selenium");

【问题讨论】:

    标签: c# visual-studio selenium selenium-webdriver


    【解决方案1】:
    1. 您可以使用 XPath contains() 函数,例如:

      //*[contains(@id,'FirstName')]
      
    2. 如果由于某种原因它不起作用,即返回多个匹配项,您还可以通过使用 substring()string-length() 函数的组合来实现某种形式的 ends-with() 函数,例如:

      //*[substring(@id, string-length(@id) - string-length('FirstName')+ 1, string-length(@id))= 'FirstName']
      

    更多信息:XPath Operators & Functions

    【讨论】:

      【解决方案2】:

      您可以按如下方式更改您的 xpath:

      driver.FindElement(By.Xpath("//*[contains(@id,'FirstName')]"));
      

      【讨论】:

        【解决方案3】:

        你可以使用 contains()

        driver.FindElement(By.XPath("//*[contains(@id,'FirstName')]")).SendKeys("Selenium");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-08-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-13
          • 1970-01-01
          • 2011-08-25
          相关资源
          最近更新 更多