如何使用selenium从下拉列表中选择一个值?

如何使用selenium从下拉列表中选择一个值?,第1张

如何使用selenium从下拉列表中选择一个值?

在您的第一个选项中,硒明确指出 Element应该是“ select”但为“ option”
,这意味着您在提供

xpath
for的
option
同时仅
xpath
希望选择。

不需要使用您提供的其他选项,只需使用以下第一个选项即可:-

Select elm = new Select(driver.findElement(By.id("type")));elm.selectByVisibleText("Date");

要么

ByIndex

elm.selectByIndex(2);

要么

ByValue

elm.selectByValue("1");

如果不幸的是您的第一个选项不起作用,我希望您使用第三个选项使用

JavascriptExecutor
,如下所示:-

WebElement select = driver.findElement(By.id("type"));((JavascriptExecutor)driver).executescript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, "Date");

希望它能帮助您… :)



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5015990.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-15
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存