0

I am trying to find web elements using JavaScript. First element is being found. When I use similar code to find another element, I get null ref. exception. When I enter code into developer tools (F12), element is being found.

Here is the code I use:

IJavaScriptExecutor js = Driver as IJavaScriptExecutor;
IWebElement ChangeAccessRightsLink = (IWebElement)js.ExecuteScript("return document.getElementsByClassName('aaaaaaaaa')[1];");
ChangeAccessRightsLink.Click();
IJavaScriptExecutor jss = Driver as IJavaScriptExecutor; //AddNewAccessRightButton =null :(
IWebElement AddNewAccessRightButton = (IWebElement)jss.ExecuteScript("return document.getElementsByClassName('bbbbbbbbb')[0];");

3
  • Share URL or HTML so it is easy to answer it Commented Oct 4, 2017 at 11:47
  • You are kinda defeating the point of using Selenium when you use JSE like this. Why not just grab the element with Selenium? You aren't doing anything in JSE that you can't do with Selenium anyway. Commented Oct 4, 2017 at 14:11
  • I agree, @iamsanklap89. Selenium driver on C# simply couldn't find it neither using XPath, nor Class. Since I wanted it to be fixed fast, I used JavaScript. Commented Oct 6, 2017 at 6:29

1 Answer 1

1

You need a wait till your JS is not ready. Try Explicit wait, fluent wait .

Sign up to request clarification or add additional context in comments.

2 Comments

And that was absolutely correct answer! Amazing, how simple it was - adding an extra second worked out.
Glad it works for you. voted you up. You will now get a privilege to vote up posts :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.