我一直在教自己如何使用Selenium和Phantom JS,但我已经达到了约束力。
尽管经过多次尝试,我还是无法实现等待。
这是我的主要尝试:
Driverwait:
_wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 5));
地址:
private const string FACTBOOKURL =
"https://www.cia.gov/library/publications/the-world-factbook/index.html";
方法:
public static string GetValueFromKeyAndCountry(string key, string country, string category)
{
string res = string.Empty;
IWebElement dropDownCountryList = _driver.FindElement(By.XPath("//select[@name='selecter_links']"));
SelectElement countryList = new SelectElement(dropDownCountryList);
countryList.SelectByText(country);
IWebElement pE = _driver.FindElement(By.XPath("//h2[@sectiontitle='" + category + "']"));
pE.Click();
IWebElement mainElement = pE.FindElement(By.XPath("//span[contains(.,'" + key + "')]"));
Console.WriteLine("-----LOG: \n-Tagname: " + mainElement.TagName + " \n-Attribute: " + mainElement.GetAttribute("class") + " \n-Text: " + mainElement.Text);
IWebElement parent = GetParent(mainElement);
Console.WriteLine("-----LOG: \n-Tagname: " + parent.TagName + " \n-Attribute: " + parent.GetAttribute("class") + " \n-Text: " + parent.Text);
//------ This is the main part. I wait for the element to be found and I return its contents.
_wait.Until((d) =>
{
return parent.FindElement(By.XPath(".//span[@class='category_data']"));
});
mainElement = parent.FindElement(By.XPath(".//span[@class='category_data']"));
return mainElement.Text;
}
方法调用:
string president =
PropertyManager.GetValueFromKeyAndCountry("head of government: ", "Colombia", "Government");
网页结构:
我不想使用隐含的等待。我希望等待元素可用,但此代码似乎不正确。我无法告诉我失踪的是什么。
任何指针都会受到高度赞赏,甚至是一种替代解决方案(当然不是隐含的等待)。