最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Selenium not loading images - Stack Overflow

programmeradmin7浏览0评论

I'm using selenium with chrome-driver to automate some tasks, but it is not loading the images. I've checked the code, but can't why the user-profile could force selenium to not load the images. I'm using user-data argument to avoid losing the session when I open or close the chrome driver.

If i comment/uncomment this line, the images load or not:

self.chrome_options.add_argument("--user-data-dir=C:\\Users\\[username]\\AppData\\Local\\Google\\Chrome\\User Data\\Default")

Why? How can I solve it? thanks

This is how I am calling selenium:

driver=WD.CustWebDriver(temp_download_dir)
driver=driver.start_driver()

And this is the CustWebDriver class

class CustWebDriver:

    def __init__(self,temp_download_dir):
        self.temp_download_dir=temp_download_dir

        #remove headless to see what is happening
        self.chrome_options = Options()
        if browser_headless==True:
            self.chrome_options.add_argument("--headless")
            userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"
            self.chrome_options.add_argument(f'user-agent={userAgent}')
        self.chrome_options.add_argument("--user-data-dir=C:\\Users\\[Username]\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
        self.chrome_options.add_argument("--window-size=1920x1080")
        self.chrome_options.add_argument("--disable-notifications")
        self.chrome_options.add_argument('--no-sandbox')
        self.chrome_options.add_argument('log-level=1')

        
        self.chrome_options.add_experimental_option("prefs", {
                "download.prompt_for_download": False,
                "download.directory_upgrade": True,
                "safebrowsing_for_trusted_sources_enabled": False,
                "safebrowsing.enabled": False,
               
        })
        self.chrome_options.add_argument('--disable-gpu')
        self.chrome_options.add_argument('--disable-software-rasterizer')
        self.chrome_options.add_experimental_option("detach", True)
    def start_driver(self):
        try:
            driver = webdriver.Chrome(options=self.chrome_options)

            drivermand_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
            params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': self.temp_download_dir}}
            driver.execute("send_command", params)

            return driver
        except Exception as e:
            print(e)
            sys.exit("Chromedriver Error")

I'm using selenium with chrome-driver to automate some tasks, but it is not loading the images. I've checked the code, but can't why the user-profile could force selenium to not load the images. I'm using user-data argument to avoid losing the session when I open or close the chrome driver.

If i comment/uncomment this line, the images load or not:

self.chrome_options.add_argument("--user-data-dir=C:\\Users\\[username]\\AppData\\Local\\Google\\Chrome\\User Data\\Default")

Why? How can I solve it? thanks

This is how I am calling selenium:

driver=WD.CustWebDriver(temp_download_dir)
driver=driver.start_driver()

And this is the CustWebDriver class

class CustWebDriver:

    def __init__(self,temp_download_dir):
        self.temp_download_dir=temp_download_dir

        #remove headless to see what is happening
        self.chrome_options = Options()
        if browser_headless==True:
            self.chrome_options.add_argument("--headless")
            userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"
            self.chrome_options.add_argument(f'user-agent={userAgent}')
        self.chrome_options.add_argument("--user-data-dir=C:\\Users\\[Username]\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
        self.chrome_options.add_argument("--window-size=1920x1080")
        self.chrome_options.add_argument("--disable-notifications")
        self.chrome_options.add_argument('--no-sandbox')
        self.chrome_options.add_argument('log-level=1')

        
        self.chrome_options.add_experimental_option("prefs", {
                "download.prompt_for_download": False,
                "download.directory_upgrade": True,
                "safebrowsing_for_trusted_sources_enabled": False,
                "safebrowsing.enabled": False,
               
        })
        self.chrome_options.add_argument('--disable-gpu')
        self.chrome_options.add_argument('--disable-software-rasterizer')
        self.chrome_options.add_experimental_option("detach", True)
    def start_driver(self):
        try:
            driver = webdriver.Chrome(options=self.chrome_options)

            drivermand_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
            params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': self.temp_download_dir}}
            driver.execute("send_command", params)

            return driver
        except Exception as e:
            print(e)
            sys.exit("Chromedriver Error")
Share Improve this question edited Mar 24 at 15:43 Cristian Avendaño asked Mar 24 at 15:24 Cristian AvendañoCristian Avendaño 4878 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Keep things simple! - Remember Selenium is being used successfully by millions of others worldwide.

The main issues will always be your setup or configuration of Selenium or local network connection.

  1. First remove all the chrome arguments you put and use the default and standard way of using Selenium without any options. Use ChatGPT or AI chat bot to give you a basic and simple script in python.

  2. Then modify it to use against your website

  3. Look at the error you get on the browser and if its network related due to certificates then you can add in the arguments for the browser. Again use ChatGPT or AI chat bot to give you this basic addition.

  4. If that still doesn't work then you need to look at the LAN settings of your local machine and get it resolved. It could be due to corrupt or non-existing proxy settings

发布评论

评论列表(0)

  1. 暂无评论