亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

selenium click() 無法關閉彈出窗口

selenium click() 無法關閉彈出窗口

胡子哥哥 2024-01-16 15:16:31
為了學習硒,我一直在開發一個假的“投注機器人”,但我無法關閉有時會出現在我想從中獲取賠率的網站上的彈出窗口。我的方法是使用該函數submit_bets();過濾后的游戲列表,格式為:"League|team 1|team 2|Date|Probability in %|and prediction(1,X or 2)"我從這里獲取數據。然后,對于每場過濾后的比賽,我打開投注網站上的聯賽投注頁面,瀏覽那里的所有比賽以找到過濾后的比賽并獲得真實賠率。對于filtered_games 中的每個過濾游戲,我需要打開投注網站的頁面,如果出現彈出窗口,我無法獲取數據。def submit_bets(filtered_games):driver = webdriver.Chrome(PATH)f=codecs.open("bets.txt","r", encoding='utf-8')for line in filtered_games:    l=line.split("|")    print(l)    driver.get(leagues_to_links.get(l[0]))    scroll_down(driver)    time.sleep(2)    try:        button = driver.find_element(By.XPATH, "/html/body/div[1]/div/section[2]/div[7]/div/div/div[1]/button" )        driver.execute_script("arguments[0].scrollIntoView(true)", button)        button.click()    except:        print("no button")    games=driver.find_elements_by_class_name("events-list__grid__event")    for i in games:        game=str(i.text).split("\n")        try:            if forebet_teams_to_betano.get(l[1]) in game[2] and forebet_teams_to_betano.get(l[2]) in game[3]:                print(game)                if str(l[5]) == "1":                    print("1")                    print(str(game[7]))                elif str(l[5]) == "X":                    print("X")                    print(str(game[9]))                else:                    print("2")                    print(str(game[11]))        except:            print("")在此鏈接中,您可以找到彈出窗口出現時頁面的 html:帶有 html 的 Github 頁面在此鏈接中您可以找到頁面文件,有時您可能需要刷新才能彈出感謝您抽出時間,并隨時留下任何建議來改進我的代碼。
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

我的解決方案:


#Closing popup for Portugese betting site


from selenium import webdriver

from selenium.webdriver.firefox.options import Options


URL = "https://www.betano.pt/sport/futebol/ligas/17083r/"    


# Browser options

options = Options()

options.headless = True

firefox_profile = webdriver.FirefoxProfile()

firefox_profile.set_preference("browser.privatebrowsing.autostart", True)


browser = webdriver.Firefox(firefox_profile=firefox_profile)

browser.get(URL)


##### Copy this part into your own code #####

try:

    browser.find_element_by_xpath('//button[@class="sb-modal__close__btn uk-modal-close-default uk-icon uk-close"]').click() # Click pop-up close button

    print("Pop-up closed.")

except:

    print("Pop-up button not found.")

#########

關閉此彈出窗口:

https://img1.sycdn.imooc.com/65a62d9d000166d712841060.jpg

請記住,這依賴于通過非常具體的類名稱來查找按鈕。您需要將最后的 try- except 調整為您自己的代碼。



查看完整回答
反對 回復 2024-01-16
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號