How to click a “select option” and then evaluate loaded content with casperjs

后端 未结 9 1523
长情又很酷
长情又很酷 2021-01-02 01:46

I\'m trying to crawl the sizes for this product:

Link to product

The problem: The sizes are loaded after the color of the product is selected.

In th

9条回答
  •  感动是毒
    2021-01-02 02:09

    A slightly hacky way to do this without using jQuery utilising built-in casper methods is:

    // Assumes the select box is on the first item at index 0
    chooseSelectOption = (friendlyName : string, selectLocator : string, optionIndex : number) => {
        casper.test.assertExists(selectLocator, "then select index " + optionIndex + " in the " + friendlyName + " select box");
        casper.click(selectLocator);
        this.goDown(selectLocator, optionIndex);
    };
    
    // recursive funtion to go down various levels
    private goDown = (locator: string, depth : number, currentLevel : number = 0) => {
        if (currentLevel >= depth) { return; }
        casper.sendKeys(locator, casper.page.event.key.Down, { keepFocus: true });
        this.goDown(locator, depth, currentLevel + 1);
    };
    

    This is in TypeScript but you can edit for vanilla JS if you need to. You need to use a recursive function because a normal for loop gets into difficulties with capser's queuing system.

提交回复
热议问题