3

Target URL: http://www.supremenewyork.com/shop/jackets/uaxjeqvro/fm9kozqa6

Target Element: #s

Problem: Can't select a value from the drop down. I've tried multiple things, only related question I could find on Stack Overflow was this,[How to select an option from dropdown select but none of these answers describe how to select the option via the text of the element rather than the value of the option.

Cody G
  • 8,368
  • 2
  • 35
  • 50
Sunstro
  • 75
  • 2
  • 4
  • 9

1 Answers1

4

This should work, tested with version 1.7.0 on https://try-puppeteer.appspot.com/

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();
Cody G
  • 8,368
  • 2
  • 35
  • 50
  • Is there a way to do it without the value? Like for example by text? – Sunstro Aug 23 '18 at 20:41
  • That’s a funny link! Sorry if I wasn’t explicit, the values on those are dynamic and aren’t the same for every product, but the size names (medium, large) are. So it’s more convenient to go by that then trying to go by value. I appreciate your help! – Sunstro Aug 23 '18 at 21:44
  • Np! Learn new things every day, this helped me get more familiar with puppeteer :) – Cody G Aug 23 '18 at 23:26
  • Is there any other way I can reach you, I have just one more question :p – Sunstro Aug 24 '18 at 02:55
  • You can post another question, and then link it here in the comments! Or just ask it here if it's basic. Also, since you're new; you can mark a question as accepted _and_ upvote it if you found it helpful. – Cody G Aug 24 '18 at 03:25
  • Well I'm pretty good with JS, and i'm getting better at Node, but is their a place you recommend where I can read and learn more about web automation in Node. I'm very familiar with it in Python, but that's a different beast. I understand parts of your answer, but some parts I don't. I'd like to learn rather than just copy and paste. – Sunstro Aug 27 '18 at 18:00
  • Puppeteer is pretty much the bleeding edge, I'm not quite sure there's a place you can learn too much about it besides their github and watching the development very closely. If you really want to dive into it, puppeteer is just a high level api on top of https://chromedevtools.github.io/devtools-protocol/ , so if you want to do "anything" you'd probably want to use that. Selenium is also a popular testing framework I've seen. https://seleniumhq.github.io/selenium/docs/api/javascript/ – Cody G Aug 27 '18 at 18:18
  • Well I have hit a wall in my project. In python we have a library called BS4, that allows you to parse a html webpage. I'm trying to do that with node.js, that way I can apply some if and else filtering logic. I'm just kind of stuck, but I appreciate your help. Will definitely check out the links! – Sunstro Aug 27 '18 at 18:25