问题描述
我在从下拉菜单中选择一个选项时遇到了问题。 该站点是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");