[Python crawler • selenium] FAQs about the new version of selenium4 automatically obtaining the driver


foreword

Regarding the last published article "[Python Crawler • selenium] selenium4 new version user guide", many people reported that the automatically obtained driver would cause a crash. This article gives detailed configuration.

First of all, let me talk about the problem of flash back. I installed selenium in a low version of python, and found that the old version was installed, not the version of selenium4, so the usage was still selenium3. At this time, using the selenium4 code I provided before will inevitably report an error, so in this article The codes of selenium3 and selenium4 versions of various drivers will be given for everyone to directly cv.

Secondly, this article will let everyone learn how to troubleshoot. The cause of selenium’s flashback is actually very easy to troubleshoot. Generally speaking, there will be a prompt when an error is reported during operation. These things will be explained in the text.


1. Install the driver

Current environment configuration, python=3.10,

Note, make sure your python version is a high version, the low version will automatically install selenium3, and the high version will automatically install selenium4, if you don't know what version of selenium you installed, I suggest you use the command to check the python3 I pip listuse

. 10 installed selenium, which is version 4.7 by default.

pip install webdriver-manager

Then install selenium

pip install selenium

2. Use steps

This time I take Edge as an example, because I only have Edge idle at the moment.

1. Import package

code show as below:

from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager

2. Generate driver

code show as below:

driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))

3. Open the website

Here I take opening my blog as an example, pay attention to the delay here, otherwise it will crash.

driver.get('https://blog.csdn.net/weixin_47754149?spm=1010.2135.3001.5343')
sleep(10000)

The running effect diagram is as follows

It will automatically shut down after running for 10 seconds, indicating that there is indeed no problem.

2. Handling of selenium flashback problem

Under normal circumstances, selenium flashback is caused by two problems. One is that selenium is not compatible with your code version, or your code is wrong, and the other is that the browser disappears after a flash. This is not your code. Wrong, but the code is executed when the browser is opened. You just need to do a little bit of delay. Therefore, the first case is dealt with here.

1. The selenium version does not match the code

This part is the most likely to encounter errors. Generally speaking, when you install selenium, it will automatically recognize your python version and install the corresponding version. For example, if you use python3.6, selenium3 will be installed by default. If you use python3.10, selenium4 will be automatically installed, so It is very important to determine which version of selenium you have installed.

First of all, to confirm your selenium version, use the following command

pip list

Then it will output the package and version number in your current environment.
You need to find your selenium, check which version it is

, and then according to your corresponding version number, you should write the code of selenium3 or selenium4, so that it will not crash.

2. Selenium code exception

This part of the flashback problem basically shows that your code foundation is not solid enough. This kind of problem is a grammatical error, because python is executed while explaining, the correct code will be executed, and the wrong code will not be executed after that, and some are not. The fields and properties of will not be marked during development, so it is hard for novices to see it.

My suggestion for this kind of problem is to use IDE for development, I recommend using visual studio code, then install the python plug-in, and select your python version in the status bar


At this point, the code will have a hint, if there is a problem with the code you wrote, it will be displayed here

If you have some development experience, just add exception handling directly, so I won’t go into details.

3. Code example

The code here is exclusively for cv, which makes you more comfortable and direct when you are in cv.

1. selenium4 code example

1. Chrome

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

2. Chromium

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))

3. Brave

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))

4. Firefox

# selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))

5. IE

# selenium 4
from selenium import webdriver
from selenium.webdriver.ie.service import Service as IEService
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(service=IEService(IEDriverManager().install()))

6. Edge

# selenium 4
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))

7. Opera

# selenium 3 & 4
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager

driver = webdriver.Opera(executable_path=OperaDriverManager().install())

2. Selenium3 code example

1. Chrome

# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

2. Chromium

# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())

3. Brave

# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install())

4. Firefox

# selenium 3
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

5. IE

# selenium 3
from selenium import webdriver
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(IEDriverManager().install())

6. Edge

# selenium 3
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(EdgeChromiumDriverManager().install())

7. Opera

# selenium 3 & 4
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager

driver = webdriver.Opera(executable_path=OperaDriverManager().install())

3. Provided operational framework

import os

import requests
from requests import Response

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.download_manager import WDMDownloadManager
from webdriver_manager.core.http import HttpClient
from webdriver_manager.core.logger import log

class CustomHttpClient(HttpClient):

    def get(self, url, params=None, **kwargs) -> Response:
        """
        在这里添加你的操作,session,代理等
        """
        log("这些将会被自定义的http客户端调用")
        return requests.get(url, params, **kwargs)


def test_can_get_chrome_driver_with_custom_http_client():
    http_client = CustomHttpClient()
    download_manager = WDMDownloadManager(http_client)
    path = ChromeDriverManager(download_manager=download_manager).install()
    assert os.path.exists(path)

Summarize

The above is all the content of this article, mainly to deal with the problems you encounter when using the automatic acquisition driver. Although the problem summary is not very comprehensive, it has touched the fundamental aspects, that is, the selenium and python versions must be Match to install selenium4. The second is to post some selenium codes so that everyone can directly cv. I believe this is very useful. When you read this article and find that you can directly cv, it must be very comfortable. I think so too.
'
If you have encountered a problem, and it is not the above-mentioned problem, please private message me directly.

Guess you like

Origin blog.csdn.net/weixin_47754149/article/details/128502965