C# C Selenium:将变量传递到XPath

C# C Selenium:将变量传递到XPath,c#,selenium,variables,xpath,C#,Selenium,Variables,Xpath,我试图将一个变量传递到XPath中,但我不知道如何执行此操作: string myVar = "Activities"; IWebElement webElement2 = driver.FindElement(By.XPath("//a[normalize-space(.)='Activities' and @_ngcontent-c32]")); 可以通过将变量嵌入XPath字符串来实现这一点: IWebElement webElement2 = driver.FindElement(By

我试图将一个变量传递到XPath中,但我不知道如何执行此操作:

string myVar = "Activities";
IWebElement webElement2 = driver.FindElement(By.XPath("//a[normalize-space(.)='Activities' and @_ngcontent-c32]"));

可以通过将变量嵌入XPath字符串来实现这一点:

IWebElement webElement2 = driver.FindElement(By.XPath("//a[normalize-space(.)='" + myVar + "' and @_ngcontent-c32]"));

可以通过将变量嵌入XPath字符串来实现这一点:

IWebElement webElement2 = driver.FindElement(By.XPath("//a[normalize-space(.)='" + myVar + "' and @_ngcontent-c32]"));

这个问题已经回答了。但是,我建议您可以将此方法添加到脚本中,以便在未找到xpath或xpath无效时处理NoTouchElementException,并处理其他异常

   public static bool existsElement(IWebDriver _driver,By by,int waitBySecond,string message)
    {
        WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0, 0, waitBySecond));

        wait.Message = message;

        try
        {
            // check _driver.FindElement(by) or wait element to check

        }
        catch (WebDriverTimeoutException ex)
        {
            Console.WriteLine("Message : " + ex.Message);
            Console.WriteLine("StackTrace: " + ex.StackTrace);

            return false;
        }

        catch(NoSuchElementException ex)
         {
           Console.WriteLine("Message : " + ex.Message);
           Console.WriteLine("StackTrace: " + ex.StackTrace);
          return false;
         }
        catch(Exception e)
        {
            Console.WriteLine("Message : " + wait.Message);
            Console.WriteLine("StackTrace: " + ex.StackTrace);

            return false;
        }



        return true;
    }

这个问题已经回答了。但是,我建议您可以将此方法添加到脚本中,以便在未找到xpath或xpath无效时处理NoTouchElementException,并处理其他异常

   public static bool existsElement(IWebDriver _driver,By by,int waitBySecond,string message)
    {
        WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0, 0, waitBySecond));

        wait.Message = message;

        try
        {
            // check _driver.FindElement(by) or wait element to check

        }
        catch (WebDriverTimeoutException ex)
        {
            Console.WriteLine("Message : " + ex.Message);
            Console.WriteLine("StackTrace: " + ex.StackTrace);

            return false;
        }

        catch(NoSuchElementException ex)
         {
           Console.WriteLine("Message : " + ex.Message);
           Console.WriteLine("StackTrace: " + ex.StackTrace);
          return false;
         }
        catch(Exception e)
        {
            Console.WriteLine("Message : " + wait.Message);
            Console.WriteLine("StackTrace: " + ex.StackTrace);

            return false;
        }



        return true;
    }
或者在较新的C版本上:IWebElement webElement2=driver.FindElementBy.XPath$//a[normalize space.='{myVar}'和@ngcontent-c32];或者在较新的C版本上:IWebElement webElement2=driver.FindElementBy.XPath$//a[normalize space.='{myVar}'和@ngcontent-c32];