我使用Selenium和我有以下擴展方法執行JavaScript。Selenium FindBy屬性使用Javascript和等待在C#
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);
}
}
然而,通過使用Javascript產生employerSelect,那麼有沒有辦法做這樣的事情:
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;
我能做些什麼來做這樣的事情?
你錯了,請參閱http://stackoverflow.com/questions/14263483/how-to-write-my-own-customize-locator-換硒的webdriver式的Java –