Selenium在Windows中浏览器未打开时无法找到类

-1 投票
1 回答
33 浏览
提问于 2025-04-14 16:09

我在用Python的selenium来提取数据,如果浏览器没有打开,就会出现下面的错误。但是如果浏览器开着,一切都运行得很好。我的代码在浏览器打开的情况下连续运行了超过5个小时。

现在我不能总是让浏览器开着,因为我还有其他事情要做。请看我的代码和下面显示的错误。

try:
            #interest by Geo data container
            geo_container = WebDriverWait(self.browser, random.randint(fixed_time,fixed_time*2)).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "widget[class='multi-heat-map-widget']")))
        
        except:
            geo_container = WebDriverWait(self.browser, random.randint(fixed_time,fixed_time*2)).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "widget[type='fe_geo_chart']")))

           
        #download data
        geo_download_button = WebDriverWait(geo_container, random.randint(fixed_time*2,fixed_time*4)).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".widget-actions-item.export")))
        geo_download_button.click()

[我的代码[geo_container和geo_container类[download_button和类错误信息](https://i.stack.imgur.com/fa9Dv.png)](https://i.stack.imgur.com/vQOXQ.png)](https://i.stack.imgur.com/D7ClK.png)

有没有什么办法可以解决这个问题?

1 个回答

0

你可能想要使用 headless 这个选项。

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(options = options)

撰写回答