更改 select 选项中的 selected 值
Changing selected value in a select option
我目前无法更改下拉列表的值。我目前正在 https://www.reebok.com/us/cart 上将商品添加到我的购物车。我正在尝试更改数量。
我正在尝试将我的 java 代码重写为 java 脚本。话虽如此。
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);
我目前在 java 使用 Selenium 库。但是在 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;
和
document.getElementsByClassName('gl-dropdown-native__select-element')[0].value=7;
这是更改购物篮中商品的工作函数。问题是找到他们依赖的处理程序和事件类型。这是 mouseup
和直接在 window 中的事件。
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, }));
}
希望对您有所帮助!
我目前无法更改下拉列表的值。我目前正在 https://www.reebok.com/us/cart 上将商品添加到我的购物车。我正在尝试更改数量。
我正在尝试将我的 java 代码重写为 java 脚本。话虽如此。
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);
我目前在 java 使用 Selenium 库。但是在 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;
和
document.getElementsByClassName('gl-dropdown-native__select-element')[0].value=7;
这是更改购物篮中商品的工作函数。问题是找到他们依赖的处理程序和事件类型。这是 mouseup
和直接在 window 中的事件。
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, }));
}
希望对您有所帮助!