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.

XPATH contains not working with page.$x

See original GitHub issue

I am using latest puppeteer version and trying to click on span element with text, but returns undefined.

const setting = await page.$x('//span[contains(text(), "Settings")]'); // setting is undefined and setting.click() don't work

However, I can get all span element with following const setting = await page.$x('//span'); // returns array of span elements

I am still trying to iterate over span elements to find exact span with correct textContent but still i get is undefined. const setting = spans.filter(span => span.textContent === 'Settings'); // still undefined.

Is there any easy way to find/click element with text content Same things perfectly works on selenium. Is this issue in puppeteer ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
shankarregmicommented, Dec 9, 2018

I think it is always better to waitFor element to be visible in DOM before trying to click/accessing it.

await page.waitForXPath('//span[contains(text(), "Settings")]', 5000);
const [setting] = await page.$x('//span[contains(text(), "Settings")]');
if(setting) setting.click();
0reactions
regmishcommented, Dec 9, 2018

@shankarregmi My bad I was trying to click on the settings before it was completely loaded into DOM, your solution perfectly works for me. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

XPath contains(text(),'some string') doesn't work when used ...
The following query returns the element, but it returns far more then just the element – it returns the parent elements as well,...
Read more >
How does XPath contains works? - eduCBA
Xpath Contains is an XPath function that is used to find Dynamic web elements and returns a partial attribute value to locate the...
Read more >
XPath Contains: Text, Following Sibling & Ancestor in Selenium
XPath contains is a function within Xpath expression which is used to search for the web elements that contain a particular text. We...
Read more >
Puppeteer - Xpath Functions - Tutorialspoint
The xpath for the element shall be //*[text()='Library']. Here, we are working with the xpath selector, so we have to use the method:...
Read more >
XPath Contains Text | XPath Starts With, Ends With
XPath axes in selenium are methods to identify those dynamic elements which are not possible to find by normal XPath method such as...
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