简体   繁体   中英

How to click on dynamic xpath with puppeteer?

I need to click with puppeteer on a dynamic xpath but I can't achieve to match the path.

This is the dynamic path I need to click


 //[@id="Pfb41b20eb4b64727b5db3b2abea2187d_1_oReportCell"]/table/tbody/tr/td/table[2]/tbody/tr[3]/td[1]/div/a

The dynamic part starts after the P and ends on the first _

And this is what I have tried so far


const download = await page.$x('//id[contains("1_oReportCell")]/table/tbody/tr/td/table[2]/tbody/tr[3]/td[1]/div/a');

const download = await page.$x('//*[@id^="P"][@id$="_oReportCell"]/table/tbody/tr/td/table[2]/tbody/tr[3]/td[1]/div/a
');

Can someone help me out, please.

In XPath 1.0, the ends-with() function can be expressed as:

substring($string, string-length($string) - string-length($pattern) + 1) = $pattern

In your XPath expression:

//[substring(@id, string-length(@id) - 12) = "1_oReportCell"]/table/tbody/tr/td/table[2]/tbody/tr[3]/td[1]/div/a

Edit : XPath 2.0+ has a proper ends-with() function, of course.

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