【问题标题】:selenium click() not working on closing pop-upselenium click() 无法关闭弹出窗口
【发布时间】:2021-02-09 03:10:41
【问题描述】:

为了学习 selenium,我一直在开发一个假的“投注机器人”,但我无法关闭有时会出现在我想从中获得赔率的网站上的弹出窗口。

我的做法是使用函数submit_bets();过滤后的游戏列表,格式为:

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

我从here 获取数据。然后对于每个过滤后的比赛,我打开投注网站上的联赛投注页面,并浏览那里的所有比赛以找到过滤后的比赛并获得真正的赔率。对于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("")

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

感谢您抽出宝贵时间,并随时留下任何提示来改进我的代码。

【问题讨论】:

  • 从页面发布相关的Html 并在您的帖子中弹出,而不是链接。
  • 用 html 添加到 github 的链接
  • 这是有问题的按钮吗: button = driver.find_element(By.XPATH, "/html/body/div[1]/div/section[2]/div[7]/div/ div/div[1]/button" ) 问题是什么?没有找到元素?你看到了什么错误?
  • 是 /html/body/div[1]/div/section[2]/div[5]/div/div/div[1]/button 我相信,我没有任何参考这在代码上,因为当我使用 XPATH 查找后执行 .click() 时它什么都没有

标签: python html selenium web-scraping


【解决方案1】:

我的解决方案:

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

关闭此弹出窗口:

请记住,这依赖于通过非常具体的类名找到按钮。您需要将最后的 try-except 调整为您自己的代码。

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多