Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How click on element with id containing colons in puppeteer?

Tags:

puppeteer

Button has id like this my:very:beautiful:button

<input id="my:very:beautiful:button" type="image" src="https://xxx/search_off.gif" name="my:very:beautiful:button" onmouseout="imgOff('searchBttn', this)" onmouseover="imgOn('searchBttn', this)" class="btn searchBttn" onclick="doSubmit(this, 'clearBttn')">

In puppeteer my attempt to click on this button is:

await page.click('#my\:very\:beautiful\:button');

Throws:

Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': '#my:very:beautiful:button' is not a valid selector.

With double escape characters:

await page.click('#my\\:very\\:beautiful\\:button');

Throws:

Error: No node found for selector: #my\:very\:beautiful\:button

I assume the problem is colon. Any thoughts how click on it?

like image 521
user52028778 Avatar asked Mar 22 '19 08:03

user52028778


2 Answers

The "double escaping" works. The colon should not be the problem.

The problem is most likely that the element simply has not been rendered yet. To wait for the element to be rendered first, you can use the function page.waitForSelector like this:

const selector = '#my\\:very\\:beautiful\\:button';
await page.waitForSelector(selector);
await page.click(selector);
like image 107
Thomas Dondorf Avatar answered Jan 04 '23 17:01

Thomas Dondorf


You can try an attribute selector:

await page.click('[id="my:very:beautiful:button"]');
like image 26
vsemozhebuty Avatar answered Jan 04 '23 15:01

vsemozhebuty