我安装了geckodriver,因为selenium不再与最新版本的Firefox兼容。出于这个原因,我不得不修改用于抓取网站的代码。我在下拉列表中选择项目时遇到问题。在下面的代码中,一切都工作到“browser.select”;我收到错误:'WebDriver'对象没有'select'属性。我在Mac上使用Spyder。
//------------------------------------------------------------------------------
// <auto-generated>
// Well, not really. This is just a trick to get StyleCop off my back.
// </auto-generated>
//------------------------------------------------------------------------------
browser.find_element_by_id( 'j_id112:secimSorgulamaForm:j_id142')。单击()
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()
[编辑:代码的最后一部分已编辑为以下内容,现在可以正常工作]:
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)
答案 0 :(得分:0)
您需要执行以下操作:
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类的对象。然后你可以使用以下方法:
select_by_index(index)
select_by_value(value)
select_by_visible_text(text)
如果有帮助,请告诉我