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.

How can i click on the link, which contains a text inside the span

See original GitHub issue

I am trying to click on the link, which contains a text inside the span

Code i tried

 page
      .waitForSelector('span[text=]')
     .then(() => page.click('span[text=dummy]'));

DOM

<a class="nt-card__headline" href="/test" tabindex="0" ">
              <span aria-hidden="true">
                         Some
                         <span class="white-space-pre"> </span>
                          dummy              
                         <span class="white-space-pre"> </span>
                          text to match            
                         </span>
              <span class="visually-hidden">Some dummy text to match</span>
</a>

Any suggestion how how can i do this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
vsemozhetbytcommented, Aug 22, 2020

If I understand correctly, you need to find a link by its text content to click on. If so, these are at least two ways:

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false, defaultViewport: null });
    const [page] = await browser.pages();

    const textToFind = 'More information...';

    // Way 1.

    await page.goto('https://example.org/');

    const link1 = await page.evaluateHandle(
      text => [...document.querySelectorAll('a')].find(a => a.innerText === text),
      textToFind
    );
    await link1.click();
    await page.waitFor(3000);

    // Way 2.

    await page.goto('https://example.org/');

    const [link2] = await page.$x(`//a[text()="${textToFind}"]`);
    await link2.click();
    await page.waitFor(3000);

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();
0reactions
stale[bot]commented, Jul 26, 2022

We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to click a link that has been found using a span defined ...
findElement(By.xpath("//a[contains(.,'News')]")).click();. This SO post has more details on other path functions: XPath contains(text() ...
Read more >
How to find and click a span class element using the text of a ...
I am using the latest version of Selenium in Python. How do I use driver.find_element to find, and then .click() , the checkbox...
Read more >
The Content Span element - HTML - MDN Web Docs
The <span> HTML element is a generic inline container for phrasing content, ... This element only includes the global attributes.
Read more >
Links - Usability & Web Accessibility - Yale University
When creating links, developers should use the <a> tag. The <a> tag has important accessibility features built in by default. It is keyboard...
Read more >
How to create XPath for the Link which ... - Google Groups
You <span> is not in your <a>, so //li/div/h2/a/span[contains(text(), 'YES')] won't match anything. Assuming the HTML you posted is correct, ...
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