我需要从下拉菜单中选择一个元素。

例如:

<select id="fruits01" class="select" name="fruits">
  <option value="0">Choose your fruits:</option>
  <option value="1">Banana</option>
  <option value="2">Mango</option>
</select>

1)首先我得点击它。我是这样做的:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

2)之后,我必须选择好的元素,让我们说芒果。

我尝试用inputElementFruits.send_keys(…)来做,但它不起作用。


除非您的单击触发了某种ajax调用来填充列表,否则实际上不需要执行单击。

只需找到元素,然后枚举选项,选择您想要的选项。

这里有一个例子:

from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()

你可以阅读更多: https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver

Selenium提供了一个方便的Select类来使用Select ->选项结构:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get('url')

select = Select(driver.find_element_by_id('fruits01'))

# select by visible text
select.select_by_visible_text('Banana')

# select by value 
select.select_by_value('1')

参见:

使用Selenium的Python WebDriver的正确方法是什么?

使用selenium.webdriver.support.ui.的最佳方式是使用下拉选择选择要工作的类,但有时由于设计问题或HTML的其他问题,它不能像预期的那样工作。

在这种情况下,您也可以使用execute_script()作为替代解决方案,如下所示

option_visible_text = "Banana"
select = driver.find_element_by_id("fruits01")

#now use this to select option from dropdown by visible text 
driver.execute_script("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, option_visible_text);

我尝试了很多东西,但是我的下拉是在一个表中,我不能执行简单的选择操作。只有下面的解决方案有效。在这里,我突出显示下拉elem和按下箭头,直到得到所需的值-

        #identify the drop down element
        elem = browser.find_element_by_name(objectVal)
        for option in elem.find_elements_by_tag_name('option'):
            if option.text == value:
                break

            else:
                ARROW_DOWN = u'\ue015'
                elem.send_keys(ARROW_DOWN)
from selenium.webdriver.support.ui import Select
driver = webdriver.Ie(".\\IEDriverServer.exe")
driver.get("https://test.com")
select = Select(driver.find_element_by_xpath("""//input[@name='n_name']"""))
select.select_by_index(2)

它会正常工作的

首先你需要导入Select类,然后你需要创建Select类的实例。 创建Select类的实例后,可以在该实例上执行选择方法,从下拉列表中选择选项。 下面是代码

from selenium.webdriver.support.select import Select

select_fr = Select(driver.find_element_by_id("fruits01"))
select_fr.select_by_index(0)

你不需要点击任何东西。 使用xpath或其他你选择的查找方法,然后使用发送键

举个例子: HTML:

<select id="fruits01" class="select" name="fruits">
    <option value="0">Choose your fruits:</option>
    <option value="1">Banana</option>
    <option value="2">Mango</option>
</select>

Python:

fruit_field = browser.find_element_by_xpath("//input[@name='fruits']")
fruit_field.send_keys("Mango")

就是这样。

列表项

公共类ListBoxMultiple {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.get("file:///C:/Users/Amitabh/Desktop/hotel2.html");//open the website
    driver.manage().window().maximize();


    WebElement hotel = driver.findElement(By.id("maarya"));//get the element

    Select sel=new Select(hotel);//for handling list box
    //isMultiple
    if(sel.isMultiple()){
        System.out.println("it is multi select list");
    }
    else{
        System.out.println("it is single select list");
    }
    //select option
    sel.selectByIndex(1);// you can select by index values
    sel.selectByValue("p");//you can select by value
    sel.selectByVisibleText("Fish");// you can also select by visible text of the options
    //deselect option but this is possible only in case of multiple lists
    Thread.sleep(1000);
    sel.deselectByIndex(1);
    sel.deselectAll();

    //getOptions
    List<WebElement> options = sel.getOptions();

    int count=options.size();
    System.out.println("Total options: "+count);

    for(WebElement opt:options){ // getting text of every elements
        String text=opt.getText();
        System.out.println(text);
        }

    //select all options
    for(int i=0;i<count;i++){
        sel.selectByIndex(i);
        Thread.sleep(1000);
    }

    driver.quit();

}

}

你可以很好地使用css选择器组合

driver.find_element_by_css_selector("#fruits01 [value='1']").click()

将attribute = value css选择器中的1更改为所需水果对应的值。

它与选项值工作:

from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@class='class_name']/option[@value='option_value']").click()

我希望这段代码对您有所帮助。

from selenium.webdriver.support.ui import Select

带有id的下拉元素

ddelement= Select(driver.find_element_by_id('id_of_element'))

使用xpath的下拉元素

ddelement= Select(driver.find_element_by_xpath('xpath_of_element'))

下拉元素与CSS选择器

ddelement= Select(driver.find_element_by_css_selector('css_selector_of_element'))

从下拉菜单中选择“Banana”

使用下拉索引

ddelement.select_by_index (1)

使用下拉列表的值

ddelement.select_by_value (' 1 ')

您可以使用匹配下拉菜单中显示的文本。

ddelement.select_by_visible_text(香蕉)

通过这种方式,您可以在下拉菜单中选择所有选项。

driver.get("https://www.spectrapremium.com/en/aftermarket/north-america")

print( "The title is  : " + driver.title)

inputs = Select(driver.find_element_by_css_selector('#year'))

input1 = len(inputs.options)

for items in range(input1):

    inputs.select_by_index(items)
    time.sleep(1)

根据提供的HTML:

<select id="fruits01" class="select" name="fruits">
  <option value="0">Choose your fruits:</option>
  <option value="1">Banana</option>
  <option value="2">Mango</option>
</select>

要从html-select菜单中选择<option>元素,必须使用select类。此外,当你必须与下拉菜单交互时,你必须为element_to_be_clickable()诱导WebDriverWait。

要从下拉菜单中选择<选项>,文本为Mango,你可以使用以下定位器策略之一:

Using ID attribute and select_by_visible_text() method: from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import Select select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "fruits01")))) select.select_by_visible_text("Mango") Using CSS-SELECTOR and select_by_value() method: select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.select[name='fruits']")))) select.select_by_value("2") Using XPATH and select_by_index() method: select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//select[@class='select' and @name='fruits']")))) select.select_by_index(2)

下拉菜单WITHOUT <select>

这适用于我每次面对没有<select>标签的下拉菜单

# Finds the dropdown option by its text
driver.find_element_by_xpath("//*[text()='text of the option']")

导入ActionChains模块

from selenium.webdriver.common.action_chains import ActionChains

使用ActionChains点击元素

drp_element = driver.find_element_by_xpath("//*[text()='text of the option']")
action = ActionChains(driver)
action.click(on_element=drp_element).perform()

在看了很多类似这篇文章之后,我设法想出了一个解决方案,让我可以在下拉列表中选择一个项目。我尝试了.send_keys,单击()和选择在各种方式没有成功。最后,在单击下拉列表中的项目之前,向下拉列表发送了3次click()命令。

dropMenu = browser.find_element_by_id('cmbDeviceType')
dropMenu.click()
dropMenu.click()
dropMenu.click()

deviceType = browser.find_element_by_id('cmbDeviceType_DDD_L_LBI16T0')
deviceType.click()

绝对不是很漂亮,但是很好用。

希望这能帮助到一些人。这是在Firefox 88.0.1上使用Python3.7.7完成的。

dropdown1 = Select(driver.find_element_by_name("fruits"))
dropdown1.select_by_visible_text('banana')

我用它进行所有的点击和选择,它总是有效的。对于下拉项,只需确保xpath是您想要选择的实际值。

var = WebDriverWait(driver, explicit_wait_seconds).until(
        EC.element_to_be_clickable((By.XPATH, self)))
    # added the click here.
    ActionChains(driver).move_to_element(var).click()
    perform_actions()

actions.perform()
 # Reset was required to clear it. Might be patched now.
actions.reset_actions()
for device in actions.w3c_actions.devices:
    device.clear_actions()

以下方式您可以在下拉框中选择。

select=browser.find_element(by=By.XPATH,value='path to the dropdown')
 select.send_keys("Put value here to select it")