我面临从下拉菜单中选择选项的问题。该网站https://uk.farnell.com."搜索"文本框前面有一个"全部"下拉框。我想选择";陶瓷电容器";从下拉列表。我尝试了很多方法,比如使用
- Select sel=new Select(定位器(
- Javascript执行器
- 操作
- 列表但都没有奏效
图像显示All has been click and Ceramic Capacitors is highlighted I want to select
xpath for All-.//div[@id='catContainer']
陶瓷电容器的xpath——.//option[text()='Ceramic Capacitors']
按第三个索引或可见文本选择。
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");