我现在在操纵木偶戏时遇到一个相当棘手的问题。
我在HTML中具有以下结构:
<tbody>
<tr rel="0" class="disabled" id="user6335934"
class="odd">
<!-- row tools -->
<td style="width: 102px; height: 18px;">
<a id="edit6335934" class="load_link link fancybox iframe help"
title="Modifier"
width="480"
height="310"
href="/fr/sphere/98844/utilisateurs/6335934/editer">
<img alt="Modifier" src="/sfDoctrinePlugin/images/edit.png">
</a>
<a id="stats6335934" class="load_link link help"
title="Consulter les statistiques"
href="/fr/sphere/98844/utilisateurs/6335934/statistiques">
<img alt="Consulter les statistiques"
src="/sfDoctrinePlugin/images/default.png">
</a>
<!-- download fiche d'apprentisage -->
<a class="link help fiche" title="Télécharger le rapport d'apprentissage"
href="/services-pjv/rest/manager/user/6335934/rapportApprentissage.pdf?applicationId=1">
<img alt="Télécharger le rapport d'apprentissage" src="/images/telecharger-fiche.png">
</a>
<!-- !$progdashEnabled -->
<a id="reset6335934" class="link help reset"
title="Relancer la dernière évaluation"
href="/fr/sphere/98844/utilisateurs/6335934/reinit/eval"
rel="Max Barbet">
<img alt="Relancer la dernière évaluation" src="/images/reset.png">
</a>
</td>
<!-- user checkbox -->
<td>
<input id="user_check_6335934" class="user_check" name="user_check" type="checkbox" value="6335934" />
</td>
<td>
<p class="ovh nom_groupe" style="max-width: 250px;"></p>
</td>
<td>
<p class="ovh nom_membre" style="max-width: 250px;">Barbet</p>
</td>
<td>
<p class="ovh prenom_membre" style="max-width: 250px;">Max</p>
</td>
<td>
<p class="ovh pseudo_membre" style="max-width: 300px;">max.barbet@rubrash.com#test</p>
</td>
<!-- Dernière Utilisation -->
<td>
<p class="ovh last_usage" >
11/08/2020 09:49:36
</p>
</td>
<!-- Tps Total Passé -->
<td>
<p class="ovh total_time_spent">
11min 4s
</p>
</td>
<!-- Evaluation Initiale (note) + Tps évaluation initiale -->
<!-- Evaluation Initiale (note) -->
<td>
<p class="ovh initial_evaluation_score" style="">
<span class='green'>59% - A1</span>
</p>
</td>
<!-- Tps évaluation initiale -->
<td>
<p class="ovh initial_evaluation_time">
11min 4s
</p>
</td>
<!-- Niveau initiale -->
<td>
<p class="ovh first_level" >
<span class='green'>59%</span>
</p>
</td>
<!--
###########################
Start Evaluations Data
###########################
-->
<!--
###########################
End Evaluations Data
###########################
-->
<!-- Tps d'entrainement -->
<td>
<p class="ovh learning_time"
>
</p>
</td>
<!-- Niveau Atteint -->
<td>
<p class="ovh reached_level" >
59%
</p>
</td>
<!-- !progdashEnabled -->
</tr>
<tr rel="1" class="disabled" id="user6334857"
class="even">
<!-- row tools -->
<td style="width: 102px; height: 18px;">
<a id="edit6334857" class="load_link link fancybox iframe help"
title="Modifier"
width="480"
height="310"
href="/fr/sphere/98844/utilisateurs/6334857/editer">
<img alt="Modifier" src="/sfDoctrinePlugin/images/edit.png">
</a>
<a id="stats6334857" class="load_link link help"
title="Consulter les statistiques"
href="/fr/sphere/98844/utilisateurs/6334857/statistiques">
<img alt="Consulter les statistiques"
src="/sfDoctrinePlugin/images/default.png">
</a>
<!-- download fiche d'apprentisage -->
<a class="link help fiche" title="Télécharger le rapport d'apprentissage"
href="/services-pjv/rest/manager/user/6334857/rapportApprentissage.pdf?applicationId=1">
<img alt="Télécharger le rapport d'apprentissage" src="/images/telecharger-fiche.png">
</a>
<!-- !$progdashEnabled -->
</td>
<!-- user checkbox -->
<td>
<input id="user_check_6334857" class="user_check" name="user_check" type="checkbox" value="6334857" />
</td>
<td>
<p class="ovh nom_groupe" style="max-width: 250px;"></p>
</td>
<td>
<p class="ovh nom_membre" style="max-width: 250px;">benardd</p>
</td>
<td>
<p class="ovh prenom_membre" style="max-width: 250px;">hugo</p>
</td>
<td>
<p class="ovh pseudo_membre" style="max-width: 300px;">hugo.benard@rubrash.com#test</p>
</td>
<!-- Dernière Utilisation -->
<td>
<p class="ovh last_usage" >
</p>
</td>
<!-- Tps Total Passé -->
<td>
<p class="ovh total_time_spent">
</p>
</td>
<!-- Evaluation Initiale (note) + Tps évaluation initiale -->
<!-- need blanks in the case of users with no eval -->
<td>
<p class="ovh initial_evaluation_score">
<!-- blank: Evaluation Initiale -->
</p>
</td>
<td>
<p class="ovh initial_evaluation_time">
<!-- blank: Tps évaluation initiale -->
</p>
</td>
<!-- Niveau initiale -->
<td>
<p class="ovh first_level" >
</p>
</td>
<!--
###########################
Start Evaluations Data
###########################
-->
<!--
###########################
End Evaluations Data
###########################
-->
<!-- Tps d'entrainement -->
<td>
<p class="ovh learning_time"
>
</p>
</td>
<!-- Niveau Atteint -->
<td>
<p class="ovh reached_level" >
</p>
</td>
<!-- !progdashEnabled -->
</tr>
有了p,我收到一封电子邮件(req.query.email)。我想检索与检索到的电子邮件相对应的“ ovh first_level”类的跨度内容。
当前,我的代码如下:
app.get('/', async function(req, res) {
const EMAIL_SELECTOR = '#signin_username';
const PASSWORD_SELECTOR = '#signin_password';
const SUBMIT_SELECTOR = '.linkContent';
const LOGIN_URL = 'url';
(async () => {
// set some options (set headless to false so we can see
// this automated browsing experience)
let launchOptions = { headless: false,
args: ['--start-maximized',] // this is where we set the proxy
};
const browser = await puppeteer.launch(launchOptions);
const page = await browser.newPage();
// set viewport and user agent (just in case for nice viewing)
await page.setViewport({width: 1366, height: 768});
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36');
// go to whatismycountry.com to see if proxy works (based on geography location)
await page.goto(LOGIN_URL, { waitUntil: 'domcontentloaded' })
await page.click(EMAIL_SELECTOR)
await page.keyboard.type('login');
await page.click(PASSWORD_SELECTOR);
await page.keyboard.type('password');
await page.click(SUBMIT_SELECTOR);
await page.waitForNavigation({
waitUntil: 'networkidle0',
});
const title = await page.evaluate(() => {
document.querySelector(".pseudo_membre").innerText
})
res.json({
mail : req.query.mail,
result : title
});
})();
})
我真的不知道该怎么做。
答案 0 :(得分:1)
如果我正确理解了这个问题,那么您知道
const percent = page.evaluate((email) => {
// Find element with the given email
const userElem = [...document.querySelectorAll('.pseudo_membre')].find(
(user) => user.innerText.includes(email)
);
// Bail if not found
if(!userElem) {
return false;
}
// Get the table the row in which the email was found
// and then inside of that find the needed info by the selector
userElem.closest('tr').querySelector('.ovh.first_level span').innerText
}, email); // <-- pass email into page.evaulate
答案 1 :(得分:0)
看起来像你想要的
let value = await page.evaluate(() => document.querySelector('.ovh.first_level span').innerText)