Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Python Selenium选择下拉列表_Python_Selenium - Fatal编程技术网

Python Selenium选择下拉列表

Python Selenium选择下拉列表,python,selenium,Python,Selenium,你能帮帮我吗,我正试图在下拉列表中选择一个项目。 做这件事最好的方法是什么 <div class="field-unit__field"> <select name="PLAYERS[players_id]" id="PLAYERS_players_id"><option value="" label=" "></option> <opti

你能帮帮我吗,我正试图在下拉列表中选择一个项目。 做这件事最好的方法是什么

<div class="field-unit__field">
  <select name="PLAYERS[players_id]" id="PLAYERS_players_id"><option value="" label=" "></option>
<option value="1">CRISTIANO RONALDO</option>
<option value="3">NEYMAR</option>
<option value="2">MESSI</option>
<option value="4">MODRIC</option></select>
</div>

您可以像xpath一样使用find_元素

  driver.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
从selenium.webdriver.support.select导入选择 从selenium导入webdriver ... driver=webdriver.Chromechrome\u路径驱动程序路径 sel=选择driver.find\u element\u by\u idPLAYERS\u players\u id sel.select_by_index1选择元素by indexx 选择按可视文本选择1按可视文本选择
此HTML是使用Select和Options标记构建的。Selenium具有内置的类Select来处理使用Select和options标记构建的下拉列表

代码:

参考文献

select = Select(driver.find_element_by_id("PLAYERS_players_id"))
--Use one of the following methods to select the options
select.select_by_index(0)
select.select_by_visible_text("")
select.select_by_value("")
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('PLAYERS_players_id'))
#select.select_by_index(index)
select.select_by_visible_text("MESSI")
#select.select_by_value(value)