【问题标题】:ElementClickInterceptedException in Python seleniumPython selenium 中的 ElementClickInterceptedException
【发布时间】:2021-11-03 05:52:31
【问题描述】:

我一直在尝试修复ElementClickInterceptedException,但我的方法不起作用。这是我正在使用的Link。当我在游戏时间之前单击左侧的栏时,它会打开一个类似于弹出窗口的东西,但它在同一个窗口中,如图所示... 我能够在页面上获得所需的信息。问题是我通过单击x 关闭它后,它不会循环到下一个,它给了我ElementClickInterceptedException: Message: Element is not clickable at point because another element obscures it。大多数情况下,它在第二个循环中执行此操作,但有时它会通过第二个循环并进入第三个循环,但它从未通过第三个循环。我尝试使用等待,直到,但这没有任何帮助,它只会转到Timeout。这是我正在使用的代码。

#This fetch all the games I want to scrape for into a list of selenium objects
v1 = _.find_elements_by_class_name('trOddsSection')
driver.implicitly_wait(10)
for __ in v1:
driver.implicitly_wait(10)
    
#__.find_element_by_class_name("col-0").click()
    
    
wait.until(EC.element_to_be_clickable((By.XPATH, ".//td[@class='col-0']")))
yyy = __.find_element_by_xpath(".//td[@class='col-0']")

# This click gets the pop-up thing out
yyy.click()
    
v3 = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "divStatisticsWidget")))
    

sleep(3)

try:
    driver.implicitly_wait(10)
    
    # This is where I scrape what I want from the pop-up
    a = v3.find_element_by_xpath(".//div[@id='right-container']/div[@id='content_headToHead']/div[@class='sectionFound']/div[@class='teamWinsDraws']")
    hm = a.find_element_by_xpath(".//div[@class='teamHomeWins']/span[@class='teamName']/span[@class='resultsPerc']").text
    dr = a.find_element_by_xpath(".//div[@class='teamDraws']/span[@class='teamName']/span[@class='resultsPerc']").text
    aw = a.find_element_by_xpath(".//div[@class='teamAwayWins']/span[@class='teamName']/span[@class='resultsPerc']").text
    print("Here:\t\t{}\t{}\t{}".format(hm, dr, aw))
    driver.implicitly_wait(10)
        
    a_ = v3.find_element_by_xpath(".//div[@id='right-container']/div[@id='content_headToHead']/div[@class='sectionFound']/div[@class='headToHeadHistory']")
    a__ = a_.find_elements_by_xpath(".//table/tbody/tr/td[@class='score']")
        print("H2H {}".format(a__[0].text))
        
except(NoSuchElementException):
    print("teamWinDraws class not found")

# This is where I close the pop-up window so I can move to next    
try:
    driver.implicitly_wait(10)
    sleep(4)
       
    wait.until(EC.element_to_be_clickable((By.XPATH, ".//*[@class='close-icon']")))
    g_g = driver.find_element_by_xpath(".//*[@class = 'close-icon']")
    driver.execute_script("arguments[0].click();", g_g)
except(NoSuchElementException):
    print('close-icon not found')
except(TimeoutException):
    print('Timed out')

任何人看到我做错了什么或如何解决这个问题,请告诉我。

【问题讨论】:

    标签: python python-3.x selenium web-scraping selenium-chromedriver


    【解决方案1】:

    问题说明

    我明白了,你在你的脚本中使用了很多driver.implicitly_wait(10),这基本上是一个隐式等待,应该设置一次,它会一直有效,直到驱动程序没有被内部/外部杀死方式。

    所以,基本上你可以在整个脚本中保留一个driver.implicitly_wait(10),然后将它们全部删除。

    此外,我们必须记住,我们应该根据每一行定义关闭按钮。我在下面做了这些更正。

    解决方案

    代码:-

    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    driver.implicitly_wait(50)
    driver.get("https://www.betking.com/sports/s/event/p/soccer/england/eng-premier-league/0/0")
    wait = WebDriverWait(driver, 20)
    try:
        wait.until(EC.element_to_be_clickable((By.ID, "cookieBoxClose"))).click()
    except:
        pass
    
    all_rows = driver.find_elements(By.XPATH, "//tr[contains(@class, 'trOddsSection')]")
    j = 0
    for row in range(len(all_rows)):
        elements = driver.find_elements(By.XPATH, "//tr[contains(@class, 'trOddsSection')]")
        time.sleep(2)
        elements[j].find_element_by_xpath(".//td[@class='col-0']").click()
        time.sleep(2)
        v3 = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@id, 'StatisticsWidget')]")))
        try:
            a = v3.find_element_by_xpath(".//div[@id='right-container']/div[@id='content_headToHead']/div[@class='sectionFound']/div[@class='teamWinsDraws']")
            hm = a.find_element_by_xpath(".//div[@class='teamHomeWins']/span[@class='teamName']/span[@class='resultsPerc']").text
            dr = a.find_element_by_xpath(".//div[@class='teamDraws']/span[@class='teamName']/span[@class='resultsPerc']").text
            aw = a.find_element_by_xpath(".//div[@class='teamAwayWins']/span[@class='teamName']/span[@class='resultsPerc']").text
            print("Here:\t\t{}\t{}\t{}".format(hm, dr, aw))
    
        except:
            print("teamWinDraws class not found")
            pass
    
        try:
            time.sleep(4)
            elements[j].find_element(By.XPATH, ".//following-sibling::tr//div[contains(@ng-click,'closeStatisticsBetradar')]").click()
            j = j + 1
        except NoSuchElementException:
            print('close-icon not found')
        except TimeoutException:
            print('Timed out')
    

    进口:-

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    程序输出:

    Here:       5 (20%) 6 (24%) 14 (56%)
    Here:       10 (59%)    6 (35%) 1 (6%)
    Here:       5 (33%) 4 (27%) 6 (40%)
    Here:       8 (29%) 4 (14%) 16 (57%)
    Here:       33 (61%)    14 (26%)    7 (13%)
    Here:       12 (29%)    10 (24%)    19 (46%)
    Here:       11 (33%)    12 (36%)    10 (30%)
    Here:       28 (52%)    13 (24%)    13 (24%)
    Here:       6 (23%) 4 (15%) 16 (62%)
    Here:       7 (50%) 1 (7%)  6 (43%)
    Here:       13 (45%)    8 (28%) 8 (28%)
    Here:       5 (50%) 2 (20%) 3 (30%)
    Here:       2 (11%) 3 (17%) 13 (72%)
    Here:       17 (63%)    3 (11%) 7 (26%)
    Here:       20 (59%)    7 (21%) 7 (21%)
    Here:       10 (34%)    4 (14%) 15 (52%)
    Here:       19 (37%)    19 (37%)    13 (25%)
    

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 2021-06-15
      • 2022-08-14
      • 1970-01-01
      • 2020-09-09
      • 2020-10-16
      • 1970-01-01
      • 2018-07-17
      相关资源
      最近更新 更多