question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I.click(text, context) not working with Puppeteer

See original GitHub issue

What are you trying to achieve?

I want to click a button in a test using I.click(buttonText, ‘button’)

What do you get instead?

> node_modules\\.bin\\codeceptjs run --grep "Handytarife"                                                                 
CodeceptJS v1.2.1                                                                                                         
Using test root "C:\Users\stefan.huber\projects\e2e-dashboard-client\examples\codeceptjs"                                 
                                                                                                                          
Search for Handytarife --                                                                                                 
 × When I search for "Handytarife" without specifying any details Then I will get a list of various tariffs in 3533ms     
                                                                                                                          
-- FAILURES:                                                                                                              
                                                                                                                          
  1) Search for Handytarife                                                                                               
       When I search for "Handytarife" without specifying any details Then I will get a list of various tariffs:          
     Clickable element jetzt vergleichen was not found inside element button                                              
                                                                                                                          
  Scenario Steps:                                                                                                         
                                                                                                                          
  - I.click("jetzt vergleichen", "button") at Test.Scenario (features\search-products_test.js:25:7)                       
  - I.refreshPage() at Test.Scenario (features\search-products_test.js:20:13)                                             
  - I.setCookie({"domain":".check24.de","httpOnly":false,"name":"c24cb","path":"/","secure":false,"value":"1"}) at Test.Sc
enario (features\search-products_test.js:19:13)                                                                           
  - I.amOnPage("https://www.check24.de/handytarife") at Test.Scenario (features\search-products_test.js:9:7)              
                                                                                                                                                                                                                                                                                                                                                                            
  Run with --verbose flag to see NodeJS stacktrace                                                                                                                                                                                                
                                                                                                                          
  FAIL  | 0 passed, 1 failed   // 4s                                                                                      

Provide test source code if related

const assert = require('assert')

Feature('Search for Handytarife')

Scenario(`When I search for "Handytarife" without specifying any details Then I will get a list of various tariffs`, 
async (I) => {
    const toNumber = arr => arr.map(priceAsStr => Number(priceAsStr.replace(' €', '').replace(',', '.')))

    I.amOnPage('https://www.check24.de/handytarife')

    const cookieDialog = {
        domain: '.check24.de',
        httpOnly: false,
        name: 'c24cb',
        path: '/',
        secure: false,
        value: '1'
      }   
    await I.setCookie(cookieDialog)
    await I.refreshPage()  

    // Test will fail here
    I.click('jetzt vergleichen', 'button')

    I.waitInUrl('/handytarife/vergleich')
    I.see('Handytarife im Vergleich', 'h1')
    I.seeElement('//filter')
    I.seeElement('product-item')
})

As far as I can see in lib/helper/puppeteer the problem is in findElements(…) and the following code will fix the above problem:


async function findElements(matcher, locator) {
  // console.log('findElements', await (await matcher.getProperty('innerText')).jsonValue(), locator)
  locator = new Locator(locator, 'css');
  if (!locator.isXPath()) {

    const matcherInnerText = await (await matcher.getProperty('innerText')).jsonValue();
    if (matcherInnerText.indexOf(locator) >= 0) return [matcher]

    return matcher.$$(locator.simplify());
  }

  return matcher.$x(locator.value);
}

Details

  • CodeceptJS version: 1.2.1
  • NodeJS Version: 8.9.4
  • Operating System: Windows
  • Puppeteer
  • Configuration file:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sergejkaravajnijcommented, Jun 30, 2018

You are trying to click span element, and it’s not generally clickable. So, you should use explicit way to point the element

This will do the trick (I used xpath to make finding text easier)

Feature('Click')

Scenario(`Test click(text, context) with puppeteer`,
  async (I) => {
    I.amOnPage('https://www.check24.de/versicherungen/')
    I.click("//*[contains(text(), 'KFZ-Versicherung')]", '.box');
  })
0reactions
panpan-lincommented, Apr 22, 2020

Came across the same issue because the text is in a div rather than a clickable element such as a button. Solved by specifying the element to click instead of the text content itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I.click(text, context) not working with Puppeteer #1127 - GitHub
I want to click a button in a test using I.click(buttonText, 'button') What do ... I.click(text, context) not working with Puppeteer #1127.
Read more >
Unable to Click Button Using Puppeteer - Stack Overflow
click (); Here is my script: const puppeteer = require( 'puppeteer' ); ( async() => { var url = 'https://www.example.com'; const browser =...
Read more >
puppeteer - npm
A high-level API to control headless Chrome over the DevTools Protocol. Latest version: 19.3.0, last published: 12 days ago.
Read more >
Web Scraping with a Headless Browser: A Puppeteer Tutorial
In this article, Toptal Freelance JavaScript Developer Nick Chikovani shows how easy it is to perform web scraping using a headless browser.
Read more >
Testing with Puppeteer - CodeceptJS
It's readable and simple and works using Puppeteer API! # Setup. To start you need CodeceptJS with Puppeteer packages installed. npm install ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found