Geckodriver:“WebDriver”对象没有属性“select”

2024-05-02 14:00:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我安装geckodriver是因为selenium不再与Firefox的最新版本兼容。因为这个原因,我不得不修改网站代码。我在下拉列表中选择项目时遇到问题。在下面的代码中,一切正常,直到“浏览器.select“我得到错误:‘WebDriver’对象没有属性‘select’。我在Mac上用Spyder。在

import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import     DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] =  '/Applications/anaconda/lib/python3.6/site-packages/selenium/webdriver/firefox'

browser = webdriver.Firefox(capabilities=firefox_capabilities)

browser.get("https://sonuc.ysk.gov.tr/module/GirisEkrani.jsf")

time.sleep(2)
browser.find_element_by_id('closeMessageButton').click()   

         browser.find_element_by_id('j_id112:secimSorgulamaForm:j_id115:secimSecmeTa   ble:0:secimId').click()

在browser.find\u element\u id('j_id112:secimSorg公司乌拉玛形态:j逯id142')。单击()

环行各省

^{pr2}$

[编辑:代码的最后一部分已编辑为以下内容,现在可以使用]:

iller = browser.find_element_by_id("j_id48:j_id49:j_id108:cmbSecimCevresi")
iller_options = iller.find_elements_by_tag_name('option')
i_options = {option.text.strip(): option.get_attribute("value")
         for option in iller_options if option.get_attribute("value").isdigit()}

for k in sorted(list(i_options.keys()))[4:81]:
# iller
    iller = browser.find_element_by_id("j_id48:j_id49:j_id108:cmbSecimCevresi")
    iller_options = iller.find_elements_by_tag_name('option')
    i_options = {option.text.strip(): option.get_attribute("value")
             for option in iller_options if option.get_attribute("value").isdigit()}
    iller_select = Select(iller)
    iller_select.select_by_value(i_options[k])

    time.sleep(5)

Tags: browseridgetbyvalueseleniumelementfind
1条回答
网友
1楼 · 发布于 2024-05-02 14:00:00

您需要执行以下操作:

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

selectEle = driver.find_element_by_id('<id_of_select_control_containg_these_option>')

select = Select(selectEle )

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

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

因此,要在seleniumwebdiver中首先使用select方法,您需要创建select类的对象。然后可以使用以下方法:

^{pr2}$

如果有用请告诉我

相关问题 更多 >