更改选择选项中的选定值

Jus*_*J72 5 javascript dropdown

我目前在更改下拉列表的值时遇到问题。我目前正在https://www.reebok.com/us/cart上向我的购物车添加商品。我正在尝试更改数量。

我正在尝试将我的 java 代码重写为 javascript。话虽如此。

Select select = new Select(driver.findElement(By.xpath("//* [@id=\"app\"]/div/div/div/div/div[2]/div/main/div[1]/div/div/div/div/div/div[2]/div[2]/div[2]/div/div/select")));
select.selectByVisibleText(5);
Run Code Online (Sandbox Code Playgroud)

目前使用 Selenium 库在 Java 中为我工作。但是在 Javascript 中,我无法模拟相同的步骤。我已经尝试了一切。

编辑:我试过了

var select= document.evaluate('//*[@id="app"]/div/div/div/div/div[2]/div/main/div[1]/div/div/div/div/div/div[2]/div[2]/div[2]/div/div/select', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
select.selectedIndex=7;
Run Code Online (Sandbox Code Playgroud)

document.getElementsByClassName('gl-dropdown-native__select-element')[0].value=7;
Run Code Online (Sandbox Code Playgroud)

Dra*_*g13 1

这是更改购物篮中的项目的工作函数。问题是找到它们所依赖的处理程序和事件类型。它是mouseup直接在窗口中发生的事件。

function changeItemTo(itermNumber) {
    const itemsSelector = '.gl-dropdown-custom__options button';
    const itemsElements = document.querySelectorAll(itemsSelector);
    if (itemsElements == null) throw new Error(`Selector ${itemsSelector} is wrong, nothing found`);
    const buttonForItem = itemsElements[itermNumber];
    if (buttonForItem == null) throw new Error(`Item with index: ${itermNumber} not found`);

    buttonForItem.dispatchEvent(new Event('mouseup', { bubbles: true, cancelable: true, }));
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!