marwaha.ks
marwaha.ks

Reputation: 549

Get location of element using selenium webdriver c#

I cannot get the location of the element. This is what i have so far I have defined the element

[FindsBy(How = How.Id, Using = "column-a")] 
private IWebElement _columnA;
private string [] startLoc;
private string [] endLoc;

Then within the method I have:

startLoc[0] = _columnA.Location.X.ToString() + " " + _columnA.Location.Y.ToString();

Upon runtime, I get an exception of type 'System.NullReferenceException' occurred in framework.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

Upvotes: 0

Views: 3005

Answers (1)

AutomatedChaos
AutomatedChaos

Reputation: 7500

Well, you cannot use a string array that is not instantiated:

// Don't do this:
string[] foo;
foo[0] = "bar";     // throws System.NullReferenceException

To initialize an array, use the string array initializer:

var foo = new string[1]; // string array with 1 empty element
foo[0] = "bar";

But in C# it is more elegant to use Collections. Maybe you can redesign your code and use a List<string>.

Upvotes: 1

Related Questions