【问题标题】:Close pop up Window Python Selenium关闭弹出窗口 Python Selenium
【发布时间】:2021-03-11 13:19:05
【问题描述】:

我正在尝试使用 Selenium 访问此网站上的“下载 csv”按钮。 https://fplreview.com/team-planner/#forecast_table。当我第一次点击该网站时,我需要输入一个“团队 ID”并点击提交,这很好,但随后会出现一个弹出广告,我无法关闭它。我尝试了一些主要使用 XPATH 的方法,但它说按钮不存在,尽管我添加了一个睡眠计时器来等待页面加载等。我的最终目标是使用请求来做到这一点,但我正在尝试让它工作首先使用硒。谢谢。下面是我的代码

`
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get('https://fplreview.com/team-planner/#forecast_table')
    
    team_id = driver.find_element_by_name('TeamID')
    team_id.send_keys(123)
    team_id.submit()
    
    # click close button on ad.
    ad_path = '//*[@id="butt"]/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/div[18]/div/div/div[3]/button'
    button = driver.find_element_by_xpath(ad_path)
    button.click()
    
    # export csv
    export_button = driver.find_element_by_id('exportbutton')
    export_button.click()
    
    driver.quit()

`

由此产生的错误

`

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="butt"]/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/div[18]/div/div/div[3]/button"}

`

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    嘿,所以我尝试使用此解决广告问题

    button = driver.find_element_by_xpath("//*[@id='close']")
    sleep(1)
    button.click()
    

    【讨论】:

      【解决方案2】:

      我自己试了一下,也遇到了同样的错误。似乎程序没有等待足够长的时间,对我来说它甚至在按钮加载之前就崩溃了,这可能导致了错误。无论您在哪里设置webdriver.implicitly_wait('time')它都会在按钮加载之前崩溃。我建议使用显式等待,如下所示:Here。遗憾的是我自己没有来使用它,所以我不能建议如何准确地做到这一点,但似乎这是解决这个问题的正确方法

      【讨论】:

        【解决方案3】:

        确保您找到了正确的关闭按钮。这将找到所有 id 为“close”的元素并单击第二个元素。尝试关闭第一个元素将引发“ElementNotInteractableException”。

        button = driver.find_elements_by_xpath("//*[@id='close']")
        button[1].click()
        

        使用显式等待和路径来纠正按钮

        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC
        from selenium.webdriver.common.by import By
        
        path = "//div[@id='orderModal_popop']/div[@class='modal-dialog']/div[@id='teamcolours']/div[@class='modal-header']/button[@id='close']"
        
        button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable(
                (By.XPATH, path)
            )
        )
        

        这会等待 10 秒,以便正确的关闭按钮可以点击。

        【讨论】:

          【解决方案4】:

          需要多次等待才能使其工作:

          from selenium import webdriver
          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.support import expected_conditions as EC
          from selenium.webdriver.common.by import By
          import time
              
          driver = webdriver.Chrome()
          driver.get('https://fplreview.com/team-planner/#forecast_table')
          
          team_id = driver.find_element_by_name('TeamID')
          team_id.send_keys(123)
          team_id.submit()
          
          # wait for the ad to load
          WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, 'orderModal_popop')))
          
          # hide the ad
          driver.execute_script("jQuery('#orderModal_popop').modal('hide');")
          
          # export csv
          WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/button[6]')))
          export_button = driver.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/button[6]')
          export_button.click()
          
          # wait for download
          time.sleep(3)
          
          driver.quit()
          

          【讨论】: