簡體   English   中英

從 Java Selenium 的下拉菜單中選擇一個選項

[英]Select an option from drop down in Java Selenium

我在從下拉列表中選擇一個選項時遇到問題。 該網站是https://uk.farnell.com 在搜索文本框之前有一個全部下拉菜單。 我想從下拉菜單中選擇“陶瓷電容器”。 我嘗試了多種方式,例如使用

  1. 選擇 sel = 新選擇(定位器)
  2. Javascript執行器
  3. 行動
  4. 列表但沒有一個工作。

圖像顯示所有已被單擊,陶瓷電容器突出顯示,我想選擇

所有人的 xpath - .//div[@id='catContainer']

陶瓷電容器的 xpath - .//option[text()='Ceramic Capacitors']

按第 3 個索引或按可見文本選擇。

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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM