壁虎驱动程序:'WebDriver'对象没有属性'select'



我安装了geckodriver,因为硒不再与最近版本的firefox兼容。因此,我不得不修改用于刮擦网站的代码。我在下拉列表中选择项目遇到困难。在下面的代码中,所有内容都需要直到" browser.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_element_by_id('j_id112:secimsorgulamaform:j_id142'(。单击((

循环通过省

time.sleep(4)
il_sayisi =   len(browser.find_element_by_id("j_id48:j_id49:j_id108:cmbSecimCevresi").find_elements_by_tag_name('option'))-1
for j in range(1,il_sayisi):
    j = j +1
    iller =     browser.find_element_by_id("j_id48:j_id49:j_id108:cmbSecimCevresi").find_elements_by_tag_name('option') 
    browser.select_dropdown('j_id48:j_id49:j_id108:cmbSecimCevresi',iller[j].value)
time.sleep(2) 
ilce_sayisi =     len(browser.find_element_by_id("j_id48:j_id49:j_id120:cmbIlceSecimKurulu").find_elements_by_tag_name('option'))-1
for i in range(0,ilce_sayisi):
    i = i + 1 
    ilceler = browser.find_element_by_id("j_id48:j_id49:j_id120:cmbIlceSecimKurulu").find_elements_by_tag_name('option')
    browser.select('j_id48:j_id49:j_id120:cmbIlceSecimKurulu',ilceler[i].value)
    time.sleep(5) 
    browser.find_element_by_id('j_id48:j_id49:j_id192').click()
    time.sleep(5) 
    browser.find_element_by_id("j_id48:tabloBilgileriPanel:j_id440").click()
    time.sleep(5) 
    browser.find_element_by_id("j_id1114:j_id1115:j_id1121").click()
    time.sleep(7) 

[编辑:代码的最后一部分已编辑为以下且现在有效]:

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)

您需要做类似下面的事情:

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')

为了使用Selenium Web Diver中的选择方法,首先需要创建Select类的对象。然后,您可以使用以下方法:

select_by_index(index)
select_by_value(value)
select_by_visible_text(text)

让我知道它是否有帮助

最新更新