如何解决从Java Selenium的下拉菜单中选择一个选项
我在从下拉菜单中选择一个选项时遇到了问题。 该站点是https://uk.farnell.com。在“搜索”文本框之前有一个“全部”下拉列表。我想从下拉列表中选择“陶瓷电容器”。 我尝试过使用
- 选择sel =新选择(定位器)
- JavascriptExecutor
- 动作
- 列表 但是没有一个。
The image displays All has been clicked and Ceramic Capacitors is highlighted which I want to select
解决方法
所有人的xpath-.//div[@id='catContainer']
陶瓷电容器的xpath-.//option[text()='Ceramic Capacitors']
通过3rd索引或可见文本进行选择。
driver.get('https://uk.farnell.com/')
Select(driver.find_element_by_id('categoryIdBox')).select_by_index(3)
Select(driver.find_element_by_id('categoryIdBox')).select_by_visible_text("Ceramic Capacitors")
导入
from selenium.webdriver.support.select import Select
,
请尝试以下代码:
WebElement dropdownlist=Driver.findElement(By.id("categoryIdBox"));
Select listbox = new Select(dropdownlist);
listbox.selectByVisibleText("Ceramic Capacitors");
,
您可以尝试使用以下代码,这可能有助于解决您的问题
WebElement element = driver.findElement(By.xpath("((//div[@id='catContainer']//following-sibling::select))"));
Select selectOption = new Select(element);
selectOption.selectByIndex(3);
WebElement option = selectOption.getFirstSelectedOption();
* This is used just to verify the selected option
System.out.println(option.getText());
,
请使用以下代码:
Select options = new Select(driver.findElement(By.xpath("//select[@id='categoryIdBox']")));
options.selectByVisibleText("Ceramic Capacitors");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。