Dymond
Dymond

Reputation: 2277

send value with pageobject c# selenium

I'm trying to create a function that will find an element with text inside the dom document.

This is how I'm I trying to achieve this:

IWebElement in PageObject:

[FindsBy(How = How.XPath, Using = "//*[contains(text()")] 
IWebElement getText { get; set; }

IWebElement extension method:

public static void containsText(this IWebElement element, string value)
{
    element.FindElement(By.XPath(",'" + value + "')]"));
}

Method:

public AutomationPage findText()
{
    getText.containsText("test")
}

So what I'm trying to achive is that the method should find the entire Xpath.

Correct output should be:

"//*[contains(text(),'test')]"

but it gives me:

Could not find element by: By.XPath: //*[contains(text()

Upvotes: 2

Views: 118

Answers (1)

mrfreester
mrfreester

Reputation: 1991

This doesn't work like you're expecting. Basically, you're trying to find an element with xpath //*[contains(text() and then you're trying to find a child element with the xpath ,'test')]. Neither of which is a valid xpath.

For dynamic xpaths, it's better to use a function rather than FindsBy

Something like this:

private IWebElement GetElementContainsText(string text)
{
    return driver.FindElement(By.XPath("//*[contains(text(), '" + text + "')];
}

Long story short, you have to construct your selector before creating your By and before calling FindElement

I can understand the sadness in letting go of the FindsBy in these scenarios, as well as the sadness of not being able to use a cool extension method. I really wish I had a solution that would do things the way you're trying to do them, but from what I've seen the above is the correct approach in these cases. Cheers :)

Upvotes: 2

Related Questions