I have hunted high and low for a recent answer and tried most of the older ones (so please do not mark this is as duplicate) and every site that google presented me ..nothing works so far. I want to use the latest version of Selenium Grid (Docker image) , I have it set up and working, and I can scrape data remotely as long as I do not have an extension installed or define a user directory, as follows...
This code does work, but it runs on a local browser and not on the docker Selenium Grid.
from selenium import webdriver from selenium.webdriver import Chrome from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.desired_capabilities import DesiredCapabilities class sgrid: def init_chrome_with_cookies_local(self): chrome_options = Options() chrome_options.add_extension(r'C:\Users\charl\OneDrive\python\vidiqext\3.41.0_0.crx') chrome_options.add_argument("user-data-dir=C:/Users/charl/OneDrive/python/userdata") driver = webdriver.Chrome(chrome_options=chrome_options) return driver But when I switch to this
def init_chrome_with_cookies_remote(self): chrome_options = Options() chrome_options.add_extension(r'C:\Users\charl\OneDrive\python\vidiqext\3.41.0_0.crx') chrome_options.add_argument(r'user-data-dir=C:/Users/charl/OneDrive/python/userdata') driver = webdriver.Remote(command_executor='http://x.x.x.x:4444/wd/hub',chrome_options=chrome_options) return driver ....it crashes, and it does not matter if both the add_extension and add_argument are tried, or one then the other, they both cause the app to crash.
File "c:/Users/charl/OneDrive/python/sgrid.py", line 183, in iterate driver.get('about:blank') UnboundLocalError: local variable 'driver' referenced before assignment PS C:\Users\charl\OneDrive\dataharvest\visualstudio>
This error really just means the driver did not get configured properly as all the other code is the same. Now I understand that some solutions on StackOverflow use the DesiredCapabilities way, but I am told that this is deprecated, and none have worked either so if anyone has this working in Python 3, Selenium Grid 4 on docker, I would be so grateful to know.
https://stackoverflow.com/questions/66539383/selenium-grid-4-0-0-beta-1-docker-driver-crashes-my-python-app-when-adding-chrom March 09, 2021 at 09:07AM
没有评论:
发表评论