phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

PhantomJS is unable to find my element.

Open qaDream77 opened this issue 10 years ago • 4 comments

I have a login button but it doesn't have an id.

<input class="loginBtn" name="submit" value="Log in" tabindex="2" type="submit">

because of that document.getElementById("submit").click();

can't find the button and returns

TypeError: null is not an object (evaluating 'document.getElementById("submit").click')

  undefined:4
  :5

How do i find the element by any of the above values that i do know? Because i do not have an ID to use.

qaDream77 avatar Mar 16 '16 18:03 qaDream77

try: document.getElementsByClassName("loginBtn")[0].click(); or document.getElementsByName("submit")[0].click();

GunsAkimbo avatar Mar 16 '16 18:03 GunsAkimbo

document.getElementsByName("submit")[0].click(); works! Why did I need [0]?

qaDream77 avatar Mar 16 '16 19:03 qaDream77

document.getElementById returns a single element. document.getElementsByName returns a collection (notice the suffixed "s"), so you need [0] to reference the first element.

GunsAkimbo avatar Mar 16 '16 19:03 GunsAkimbo

Thank you for submitting this ticket!

This issue tracker primarily serves as the tool to report and track bugs, defects, and development tasks. For general question, code review request, build problem, or other related discussion, please post it to either:

If you have not read it yet, please check also a few tips on Effective Q&A.

vitallium avatar Mar 18 '16 09:03 vitallium