简体   繁体   中英

selenium click() not working on closing pop-up

I've been working on a fake "bet bot" in order to learn selenium, but I'm having trouble closing a pop up that shows up sometimes on the web site that I want to get the odds from.

My approach is to use the function submit_bets() ; a filtered games list in the form:

"League|team 1|team 2|Date|Probability in %|and prediction(1,X or 2)"

I get the data from here . Then for each of the filtered games I open the league bet page on the betting website, and go through all the games there to find the filtered game and get the real odds. For each filtered game in filtered_games I need to open the page of the bet website and if the pop up shows up, I can't get the data.

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("")

In this link you can find the html of the page when the pop up shows up:

Github page with the html

In this link you can find the page files, you might have to refresh it sometimes to get the pop up

Thank you for your time, and feel free to leave any tips to improve my code.

My solution:

#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.")
#########

Closes this popup: 在此处输入图片说明

Keep in mind this relies on finding the button by it's very specific class name. You'll need to adapt the try-except at the end into your own code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM