3

I have a button on a webpage with the following inspection:

<button type="button" class="odd-item-dropdown-confirm" data-qa="button-odd-item-dropdown-confirm">Confirm</button>

I would like to click on this button, I tried:

function clickConfirm(){
  var bntConfirm = document.querySelector('.btn.button-odd-item-dropdown-confirm');
  bntConfirm.click();
}

clickConfirm();

And got Uncaught TypeError: Cannot read property 'click' of null

5
  • Cannot read property 'click' of null, it means it can't match the element, you need to use the name of the html element, for example button.className. also you made mstake at selecting querySelector. Commented Jan 14, 2020 at 10:12
  • If you look at my inspection, the class does exist, I tried with both 'class' and 'data-qa',none work but inspecting the element in developer mode you can see that it does exist, look at the first code in my question Commented Jan 14, 2020 at 10:12
  • As has been stated many times (in multiple answers), no the class does NOT exist Commented Jan 14, 2020 at 10:14
  • In your example too, the button doesn't have classes named btn and button-odd-item-dropdown-confirm and so document.querySelector('.btn.button-odd-item-dropdown-confirm') is not finding the targetted button element that you are expecting. There is odd-item-dropdown-confirm only, and querySelector doesn't track whatever there is inside data-qa attribute like you've written. Commented Jan 14, 2020 at 10:18
  • ok I see now, thanks for your assistance. Commented Jan 14, 2020 at 10:31

4 Answers 4

7

It says what it means: the query is not matching any element and returning null. If you look at your HTML, there is no class button-odd-item-dropdown-confirm.

You should be selecting using this selector: button.odd-item-dropdown-confirm. See proof-of-concept:

// Checking
document.querySelector('button').addEventListener('click', () => {
  console.log('I am clicked');
});

clickConfirm();
function clickConfirm(){

    var bntConfirm = document.querySelector('button.odd-item-dropdown-confirm');
    bntConfirm.click();
}
<button type="button" class="odd-item-dropdown-confirm" data-qa="button-odd-item-dropdown-confirm">Confirm</button>

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

Comments

2
clickConfirm();
function clickConfirm(){

    var bntConfirm = document.querySelector('.odd-item-dropdown-confirm');
    bntConfirm.click();
}

I think this should work if you haven't used this class anywhere else, but you can always use Ids, or other things to identify the button properly.

Comments

1

this is a problem in your code: var bntConfirm = document.querySelector('.btn.button-odd-item-dropdown-confirm');

you don't have .btn class anywhere

you don't have .button-odd-item-dropdown-confirm class anywhere

Take a look at the solutions that can work

clickConfirm();
function clickConfirm(){
    // this will not work.
    // var bntConfirm = document.querySelector('.btn.button-odd-item-dropdown-confirm');
    // you don't have .btn class anywhere
    // you don't have .button-odd-item-dropdown-confirm class anywhere
    /* --------------------- */
    
    // select by class
    var bntConfirm = document.querySelector('.odd-item-dropdown-confirm');
    bntConfirm.click();
    
    // or select by data attr
    var bntConfirm = document.querySelector('*[data-qa="button-odd-item-dropdown-confirm"]');
    bntConfirm.click();
}
<button type="button" class="odd-item-dropdown-confirm" data-qa="button-odd-item-dropdown-confirm">Confirm</button>

Comments

1

Your selected class is not the correct one.

    clickConfirm();
    function clickConfirm(){

        var bntConfirm = document.querySelector('.odd-item-dropdown-confirm');
        bntConfirm.click();
    }

Comments

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.