简体   繁体   中英

Clicking a button in a webpage with javascript - error?

Noob Question; HTML is

    <button aria-label="Navigate to page 40" data-page- 
     number="40" type="button" data-ember-action="" data- 
      ember- 
      action-582="582" data-is-animating-click="true">
       40
     </button>

I thought the code would be

       document.querySelector('button.Navigate.to.page.40').click()

But apparently this isn't a valid selector... what am I doing wrong?

You need to use the selector button[aria-label="Navigate to page 40"] :

 const button = document.querySelector('button[aria-label="Navigate to page 40"]'); console.log(button);
 <button aria-label="Navigate to page 40" data-page- number="40" type="button" data-ember-action="" data- ember- action-582="582" data-is-animating-click="true"> 40 </button>

Your selector button.Navigate.to.page.40 is valid, but it means: select a <button> which has classes of Navigate , to , page , and 40 (which of course isn't the case for this HTML).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM