I am new to Selenium and C#, which we are using right now. I am also new to using the Page Object model, to make our codes readable. Though I am having troubles using the FindsBy, so my original code was this:
public static string WarningLabel(IWebDriver driver, string element, string elementtype)
{
return driver.FindElement(By.CssSelector(".col-md-12")).FindElement(By.TagName("li")).Text;
}
Now, to display the value from that element I did this:
public void userValidate()
{
var warn = GetMethods.WarningLabel(driver, "li", "Xpath");
Console.WriteLine(warn);
}
Now, I tried the xpath using the FindsBy
[FindsBy(How = How.Xpath, Using = "//div[1]/div[2]/div/div/div/div/ul/li")]
public IWebElement warnLabel { get; set; }
now I am trying to display it on console, it would give me the value 0. How do I display the correct value using the xpath? That xpath was working when I used it in the WarningLabel method.
div
s will change the absolute location of the specific warning label you want. I would first suggest using css selectors instead of xpaths if you're able. But if not, I would look into xpath functions with the goal of trying to target the specific element you want, while touching as few others along the way as possible. You want to strike a balance between it being both concise and reliable so it only targets the element you want. – Julian Jun 10 '16 at 17:27.col-md-12 li
will find a li tag name that is a child of another element that has the css class col-md-12 – Julian Jun 10 '16 at 20:24//div[contains(@class, 'col-md-12')]//li
(substituting div for whatever the actual tag name is). – Julian Jun 10 '16 at 20:27