我正在使用 Selenium,并且我有以下用于执行 javascript 的扩展方法。
private const int s_PageWaitSeconds = 30;
public static IWebElement FindElementByJs(this IWebDriver driver, string jsCommand)
{
return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(jsCommand);
}
public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(d => d.FindElementByJs(jsCommand));
}
return driver.FindElementByJs(jsCommand);
}
public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand)
{
return FindElementByJsWithWait(driver, jsCommand, s_PageWaitSeconds);
}
在我的 HomePage 类中,我具有以下属性。
public class HomePage : SurveyPage
{
[FindsBy(How = How.Id, Using = "radio2")]
private IWebElement employerSelect;
public HomePage(IWebDriver driver) : base(driver)
{
}
public override SurveyPage FillOutThisPage()
{
employerSelect.Click();
employerSelect.Submit();
m_driver.FindElement(By.)
return new Level10Page(m_driver);
}
}
但是,employerSelect 是由 Javascript 生成的,所以有没有办法做这样的事情:
public class HomePage : SurveyPage
{
const string getEmployerJsCommand= "return $(\"li:contains('Employer')\")[0];";
[FindsBy(How = How.FindElementByJsWithWait, Using = "getEmployerJsCommand")]
private IWebElement employerSelect;
public HomePage(IWebDriver driver) : base(driver)
{
}
public override SurveyPage FillOutThisPage()
{
employerSelect.Click();
employerSelect.Submit();
m_driver.FindElement(By.)
return new Level10Page(m_driver);
}
}
本质上,我想将原始 ExecuteJs 调用替换为 FindsBy 属性的一部分,例如:
const string getEmployerJsCommand = "return $(\"li:contains('Employer')\")[0];";
IWebElement employerSelect = driver.FindElementByJsWithWait(getEmployerJsCommand);
进入 FindsBy 属性的一部分,如下所示:
const string getEmployerJsCommand= "return $(\"li:contains('Employer')\")[0];";
[FindsBy(How = How.FindElementByJsWithWait, Using = "getEmployerJsCommand")]
private IWebElement employerSelect;
我可以扩展什么来做这样的事情?