0

I am writing a powershell script to login to a website. I want to use the button.click functionality but I can't find the button's id. here is what I found by inspecting it.

<div tabindex="0" role="button" class="v-button v-widget default v-button-default">
  <span class="v-button-wrap">
    <span class="v-button-caption">Login</span>
  </span>
</div>
<span class="v-button-wrap">
  <span class="v-button-caption">Login</span>
</span>
<span class="v-button-caption">Login</span>

Thanks in advance

2
  • 1
    So, there seem to be 3 elements acting as buttons, which one do you want to interact with? Commented Feb 8, 2018 at 18:28
  • @ScottMarcus Thanks for the quick reply. If I can interact with 3 of them then I will try to test them one by one. Thanks Commented Feb 8, 2018 at 18:31

1 Answer 1

1

Given that you aren't sure which one is the one you want, you could just set up an event handler that will trigger for each and then interrogate it for something that can identify it.

// Get all the elements with a class of "v-button-caption" into an array
var buttons = Array.prototype.slice.call(document.querySelectorAll(".v-button-caption"));

// Loop over the array
buttons.forEach(function(btn){
  // Set up a click event handler
  btn.addEventListener("click", function(evt){
    // Print out some identifying information about the clicked element
    console.log("You clicked the element who's parent is: " + evt.target.parentElement.nodeName);
  });
});
<div tabindex="0" role="button" class="v-button v-widget default v-button-default">
  <span class="v-button-wrap">
    <span class="v-button-caption">Login</span>
  </span>
</div>
<span class="v-button-wrap">
  <span class="v-button-caption">Login</span>
</span>
<span class="v-button-caption">Login</span>

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

10 Comments

Thanks Scott. I will try it and mark it as an answer if it works.
@Shezratnani Once you know which element is the "button", you can just call its click method: element.click().
@Shezratnani There are two span elements that have a parent element of span, so you'd need to do some more testing to know which of those it was. Only one of those two span elements has a parent that is also a span that, itself, has a parent that is a div, so you could test .parentNode.parentNode.
@Shezratnani It wouldn't be that. It would be: evt.target.parentElement.parentElement.nodeName. And, it will come back as undefined for any elements that don't have parent elements 2 levels up.
@Shezratnani Yes, that's the point. There is only one span in your HTML that has a parent.parent of div. So, now you know that it is the one that can be found with document.querySelector("div > span > span.v-button-caption").
|

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.