当前位置: 首页 > 知识库问答 >
问题:

Python-Selenium-通过下拉菜单循环-选项不可见

苏雅珺
2023-03-14
<select id="combo3" class="chosen-select" style="display: none;">
   <option value="../99AL/DAL01ZZZZZZZZZZZZZZZ_L1.htm">Todos</option>
   <option selected="selected" value="../99AL/DAL01004ZZZZZZZZZZZZ_L1.htm">ABEJORRAL</option>
   <option value="../99AL/DAL01007ZZZZZZZZZZZZ_L1.htm">ABRIAQUI</option>
   <option value="../99AL/DAL01010ZZZZZZZZZZZZ_L1.htm">ALEJANDRIA</option>
   <option value="../99AL/DAL01013ZZZZZZZZZZZZ_L1.htm">AMAGA</option>
   <option value="../99AL/DAL01300ZZZZZZZZZZZZ_L1.htm">YONDO-CASABE</option>
   <option value="../99AL/DAL01301ZZZZZZZZZZZZ_L1.htm">ZARAGOZA</option>
</select>

根据前面关于SO的问题,比如这个问题(selenium-python-drol-down menu选项值),我首先尝试了使用Selenium进行下拉菜单操作的基本方法,即以以下方式使用其select方法:

#Preamble to code
#Importing packages
import selenium
import time
from selenium import webdriver

url = https://elecciones.registraduria.gov.co:81/esc_elec_2015/99AL/DAL01004ZZZZZZZZZZZZ_L1.htm #Setting URL

driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get(url)

time.sleep(20)


from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('combo3')) #Selecting the drop-down menu by ID
select.select_by_value("../99AL/DAL01007ZZZZZZZZZZZZ_L1.htm") # Trying to select the option in drop-down menu by value. Error arises.

当选择“按值”时,我得到一个ElementNotVisibleException错误。因此,我尝试使用So上提供的其他解决方案进行一些故障排除(参见此处:使用selenium python从下拉选项中选择一个值),例如通过元素的XPath找到它,然后单击它。下面是我尝试的代码:

driver.find_element_by_xpath("//select[@id='combo3']/option[@value='../99AL/DAL01007ZZZZZZZZZZZZ_L1.htm']").click()

这里再次出现ElementNotVisibleException。所以接下来,我尝试一下这里建议的(ElementNotVisibleException:Message:element not Visible-Python3 Selenium),即实现一些等待时间,直到下拉菜单对应的元素变得可点击为止,使用以下代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='combo3']")))
element.click()

这里我得到一个TimeoutException。我以前使用Chrome的developer tools控制台检查过是否有两个或多个元素与定位器匹配,而只有一个元素(长度:1)所以我想可能是其他原因导致了超时,但我无法找到它可能是什么。

通过实现其他方法,如ec.visibility_of_element_located,我还会得到一个TimeoutException。下面是引发TimeoutException的代码:

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//select[@id='combo3']")))
select = Select(element)

正如您所看到的,我已经尝试了各种技术,试图从Municipio下拉菜单中选择不同的选项,但仍然无法找到它。如果你能告诉我我错过了什么或者做错了什么,我会非常感谢你的帮助和建议。提前道谢!

共有1个答案

汤兴生
2023-03-14

ID为“combo3”的select元素具有样式“display:none;”这使得它对硒是不可见的。您可以使用div ID'combo3_chelect'驱动元素并输入您想要的结果。

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

select_div = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@id='combo3_chosen']")))
select_div.click()
input = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.XPATH, "//input")))
input[0].send_keys("Todos")
input[0].send_keys(Keys.RETURN)

在我的示例中,我单击select_div,它打开一个带下拉框的输入。然后输入“todos”并按return加载“todos”部分。

我使用'visibility_of_any_elements_located'作为输入,因为页面上有多个输入被隐藏(另一个选择)。如果我使用'visibility_of_element_located',Selenium尝试返回第一个不可见的输入。“visibility_of_any_elements_located”将返回任何可见输入。

 类似资料:
  • 问题内容: 我的代码使用selenium从下拉菜单中选择选项。我有一个看起来像这样的代码: 这样很好。但是下拉菜单中有很多选项,我希望遍历下拉菜单中的所有项目。我准备了以下代码来遍历选项: 这是行不通的。关于如何使这样的循环工作的任何建议?我不了解python中的循环吗? 谢谢。 问题答案: 这应该为您工作。该代码将 查找元素 迭代以从下拉列表中获取所有选项 遍历列表 对于列表中的每个项目,选择当

  • 我正在尝试通过selenium驱动程序和Python来玩autologin测试。我正在使用这个站点https://invoiceaccess.pgiconnect.com/我做了什么: 例如,我需要选择,但它选择了。在我犯错的地方,谁能帮帮我?

  • 我是使用selenium ide的新手。 我已经让我的代码的所有其他部分工作。 但是我目前有一个问题,让它在下拉菜单中选择一个选项。 我为下拉列表和

  • 我试图选择和元素下拉框: 该网页是: 我尝试过: 和 然而,我似乎找不到元素来选择下拉列表中的项目。 任何帮助是值得赞赏的!

  • 你好,我正在使用selenium,并且已经成功地设置了id历史的文本字段,但是无法从

  • 我想从下面的列表中选择一个使用selenium的选项: 这里 但问题是没有列表可供选择。 在此输入图像说明 我到目前为止的代码: 需要帮助!!