我使用GUI实现硒的一些web自动化。我有几个组合框(这里举一个例子(
这是我的示例代码:
app = Tk()
def callback(*args): # should get the updated values in Combobox ?
global v_sv_league
v_sv_league = str(sv_league.get())
#List to be filled by scraper
li_leagues = []
#StringVar
sv_league = StringVar()
sv_league.trace("w", callback)
#Label
l_d_league = tk.Label(app,text='League:',bg='#1d1b29', fg='#f8f09d',font='Tahoma 10')
#Combobox
d_league = ttk.Combobox(app,textvariable=sv_league,values=li_leagues)
#Scrape
def scrape():
btn_tflist = wait.until(EC.element_to_be_clickable((By.XPATH,('/html/body/main/section/nav/button[3]'))))
btn_tflist.click()
btn_tf_filters = wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/main/section/section/div[2]/div/div/div[2]/div[2]')))
btn_tf_filters.click()
bol_scrape = True
if bol_scrape is True:
print('n Start scraping... this might take a few minutes. Please wait and dont press anything until trade_buddy is done!n')
li_leagues = []
print('Getting leagues...n')
league_dropdown_menu = wait.until(EC.element_to_be_clickable((By.XPATH,('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[7]/div'))))
league_dropdown_menu.click()
time.sleep(1)
# scrape all text
scrape_leagues = driver.find_elements_by_xpath("//li[@class='with-icon' and contains(text(), '')]")
for league in scrape_leagues:
export_league = league.text
export_league = str(export_league)
export_league = export_league.replace(',', '')
li_leagues.append(export_league)
app.mainloop()
所以基本上这只是我代码的一小部分,但这是我的一个combobox的代码。您可以看到,我将在代码中的某个时刻调用def scrape
来收集数据并填充列表li_leagues
。然而,我的组合框并没有刷新内容,而是保持为空。
对于OptionMenu,我设置了它(使用其他代码(,但我无法使用组合框。
我在这里缺少什么建议吗?
谢谢老虎机!
在列表中添加值后,尝试使用这行代码。
.....
export_league = export_league.replace(',', '')
li_leagues.append(export_league)
c_league.config(values=li_leagues)
config()
方法充当一个更新程序,当被调用时,它只更新您的小部件。
希望这能有所帮助。
干杯