[英]Select option by text, not by value with puppeteer
目標網址: http : //www.supremenewyork.com/shop/jackets/uaxjeqvro/fm9kozqa6
目標元素:#s
問題:無法從下拉列表中選擇值。 我已經嘗試了多種方法,只有在 Stack Overflow 上我能找到的相關問題是這個,[ 如何從下拉列表中選擇一個選項,但這些答案都沒有描述如何通過元素的文本而不是元素的值來選擇選項選項。
這應該有效,在https://try-puppeteer.appspot.com/上使用 1.7.0 版進行測試
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://www.supremenewyork.com/shop/jackets/uaxjeqvro/fm9kozqa6');
let $elemHandler = await page.$('#s');
let properties = await $elemHandler.getProperties();
for (const property of properties.values()) {
const element = property.asElement();
if (element){
let hText = await element.getProperty("text");
let text = await hText.jsonValue();
if(text==="Large"){
let hValue = await element.getProperty("value");
let value = await hValue.jsonValue();
await page.select("#s",value); // or use 58730
console.log(`Selected ${text} which is value ${value}.`);
}
}
}
await browser.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.