Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Selenium-Python选择下拉菜单选项值_Python_Selenium_Selenium Webdriver_Selenium Firefoxdriver - Fatal编程技术网

如何使用Selenium-Python选择下拉菜单选项值

如何使用Selenium-Python选择下拉菜单选项值,python,selenium,selenium-webdriver,selenium-firefoxdriver,Python,Selenium,Selenium Webdriver,Selenium Firefoxdriver,我需要从下面的下拉菜单中选择一个元素 <select class="chosen" id="fruitType" name="fruitType"> <option value="">Select</option> <option value="1">jumbo fruit 1</option> <option value="2">jumbo fruit 2</option> &l

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

<select class="chosen" id="fruitType" name="fruitType">
    <option value="">Select</option>
    <option value="1">jumbo fruit 1</option>
    <option value="2">jumbo fruit 2</option>
    <option value="3">jumbo fruit 3</option>
    <option value="4">jumbo fruit 4</option>
    <option value="5">jumbo fruit 5</option>
    <option value="8">jumbo fruit 6</option>
</select>
但它给了我错误的回答。
我如何才能做到这一点。

嗨,请只使用一行代码即可

// please note the if in case you have to select a value form a drop down with tag 
// name Select then use below code it will work like charm
driver.find_element_by_id("fruitType").send_keys("jumbo fruit 4");
希望它能从以下方面有所帮助:

从selenium.webdriver.support.ui导入选择 select=Selectdriver。按\u id“水果类型”查找\u元素 现在我们有许多不同的选择。 select.select_by_index4 select.select_by_可视_文本Jumbo水果4 选择。按值“4”选择值作为字符串传递值
您可以通过以下所有选项进行迭代:

element = driver.find_element_by_xpath("//select[@name='fruitType']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
    print("Value is: %s" % option.get_attribute("value"))
    option.click()

对于这个答案,我得到了以下错误驱动程序;AttributeError:'WebElement'对象没有属性'sendKeys',python很抱歉,请使用send_-keys,我是java格式的,所以这是一个打字错误,我也更新了pythonso的sendKeys,是否类似于select=Selectdriver。通过xpath//select[@id='fruitType'和@class='selected']查找元素driver.find_element_by_idfruitType.send_keysjumbo fruit 4??没有select语句不是此行select=Selectdriver。通过xpath//select[@id='FROUTTYPE'和@class='SELECTED']查找\U元素。仅此行驱动程序。通过\U idfruitType.send\键查找\U元素\U水果4;请使用通用等待驱动程序,因为错误显示ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互。在选择选项之前应用类似于等待几秒钟的内容
element = driver.find_element_by_xpath("//select[@name='fruitType']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
    print("Value is: %s" % option.get_attribute("value"))
    option.click()