在字符串中使用变量-WebDriver C#
我试图使用公共字符串作为变量,以使代码更简洁,但Selenium WebDriver始终引发以下异常: OpenQA.Selenium.WebDriverTimeoutException InvalidSelectorException:无效选择器:指定了无效或非法的选择器 我认为可以通过以下字符串插值完成:在字符串中使用变量-WebDriver C#,c#,selenium,selenium-webdriver,string-interpolation,C#,Selenium,Selenium Webdriver,String Interpolation,我试图使用公共字符串作为变量,以使代码更简洁,但Selenium WebDriver始终引发以下异常: OpenQA.Selenium.WebDriverTimeoutException InvalidSelectorException:无效选择器:指定了无效或非法的选择器 我认为可以通过以下字符串插值完成: string common = "\"[data - test - code ^= 'ABC'] > "; new WebDriverWait(Driver.Instance, T
string common = "\"[data - test - code ^= 'ABC'] > ";
new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(2)).Until(ExpectedConditions.ElementIsVisible((By.CssSelector($"{common}td.result__td.result__td--test > img"))));
…但没有快乐。有什么想法吗
谢谢Selenium无法处理像
数据-测试-代码这样的属性
<代码>数据测试代码例如(没有空格)是可以接受的。您需要找到另一种方法来定位该元素
顺便说一下,在选择器的开头有一个冗余的
“
。字符串插值语法正确。请尝试查看其结果是否相关:“[data-test-code^='ABC']>td.result\u td.result\u td--test>img“
这正是我所期望的结果,但我不确定如何检查这一点,我不明白为什么Selenium抱怨选择器无效,因为它是您试图匹配的data-test-code
或data-test-code
?只需Console.WriteLine($“{common}td.result\uuuuuutd.result\uuuuuuuu td--test>img”)
并查看它是否是您所期望的。我猜这些空格是错误复制/粘贴的结果。如果他尝试,string common=“[data test code^='ABC']>”代码>我打赌它会工作。这不是复制/粘贴错误;我真的在寻找数据-测试-代码-听起来好像不可能,所以我想我不需要使用变量,只需要重复代码:(