mardi 8 décembre 2020

I see the button but my code seems to not find it: Exception: Message: element not interactable: element has zero size

I am trying to webbscrape a website. I am using the code below to do that but randomly I get an error that looks like this:

"selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: element has zero size"

I have tried different techniques to go around this problem as seen in the code but not solved it yet. I wonder if there is other ways I can solve this issue?

MY CODE:

options = wb.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = wb.Chrome('chromedriver.exe',chrome_options=options)


pages = 0
while pages < 82:

pages = pages+1
driver.get(f'https://www.coop.se/globalt-sok/?query=coop&category=stores&page={pages}')
time.sleep(7)
# Parsar html koden
print(driver.current_url)
page = requests.get(driver.current_url)
html = page.text
soup = BeautifulSoup(html, 'lxml')

button = soup.find_all('div', class_='Grid-cell js-storeResult u-cursorPointer')

if len(button) == 0:
    continue
else:

    # loop through all stores on the current page
    for x in range(len(button)):
        
        element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, f'body > main > div.js-childLayoutContainer > div > div.Main-container.Main-container--padding > div > div > div > div > div:nth-child(2) > div.Tab-panel.js-tabPanel.is-active > div.Grid.js-storesSearchResultList > div:nth-child({x+1})')))

        try:
            element.click()
        except:
            time.sleep(10)
            element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, f'body > main > div.js-childLayoutContainer > div > div.Main-container.Main-container--padding > div > div > div > div > div:nth-child(2) > div.Tab-panel.js-tabPanel.is-active > div.Grid.js-storesSearchResultList > div:nth-child({x+1})')))
            element.click()
      
        
        driver.execute_script("window.history.go(-1)")

driver.quit()

Aucun commentaire:

Enregistrer un commentaire