import logging import knime.extension as knext from knime.extension import ConnectionPortObject from knime.api.schema import PortObjectSpec LOGGER = logging.getLogger(__name__) # Selenium from selenium import webdriver # Chrome from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager # Edge from selenium.webdriver.edge.service import Service as EdgeService from webdriver_manager.microsoft import EdgeChromiumDriverManager # Firefox from selenium.webdriver.firefox.service import Service as FirefoxService from webdriver_manager.firefox import GeckoDriverManager ### Define Chrome def chrome_browser(): options = webdriver.ChromeOptions() if False: options.add_argument("--headless") options.add_argument("--no-sandbox") options.add_argument("--start-maximized") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-gpu") options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") options.add_experimental_option("detach", True) chrome_driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options) return chrome_driver ### Define Edge def edge_browser(): options = webdriver.EdgeOptions() options.add_argument("--no-sandbox") options.add_argument("--headless") options.add_argument("--start-maximized") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-gpu") options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") edge_driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()), options=options) return edge_driver ### Define Firefox def firefox_browser(): options = webdriver.FirefoxOptions() options.add_argument("--no-sandbox") options.add_argument("--headless") options.add_argument("--start-maximized") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-gpu") options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") firefox_driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=options) return firefox_driver ### Define Parameters @knext.parameter_group(label="Browser Selection") class browser_selection: browserSelectionChoices = ["Chrome", "Edge", "Firefox"] browserSelection = knext.StringParameter(label="Browser Selection", default_value=browserSelectionChoices[0], enum=browserSelectionChoices) @knext.parameter_group(label="Browser Configuration") class browser_configuration: headlessSelectionChoices = ["Yes", "No"] headlessSelection = knext.StringParameter(label="Use headless Browser?", default_value=headlessSelectionChoices[0], enum=headlessSelectionChoices) maximizedSelectionChoices = ["Yes", "No"] maximizedSelection = knext.StringParameter(label="Use headless Browser?", default_value=maximizedSelectionChoices[0], enum=maximizedSelectionChoices) urlSelection = knext.StringParameter(label="URL", default_value="https://google.com") @knext.parameter_group(label="Advanced Settings") class add_args: additionalArguments = knext.StringParameter(label="Additional Arguments", default_value="") ### Define Category category = knext.category( path="/community", level_id="Selenium", name="Selenium Extension", description="Selenium Python Extension", icon="icon.png" ) class MyPortObjectSpec(knext.PortObjectSpec): def __init__(self) -> None: super().__init__() #self._spec_data = spec_data def serialize(self) -> dict: return @classmethod def deserialize(cls, data: dict) -> "MyPortObjectSpec": cls(data["spec_data"]) class BrowserSessionObject(ConnectionPortObject): def __init__(self, spec: MyPortObjectSpec) -> None: super().__init__(spec) def to_connection_data(self) -> any: return super().to_connection_data() @classmethod def from_connection_data(cls, spec: MyPortObjectSpec, data: any) -> ConnectionPortObject: return super().from_connection_data(spec, data) myPortType = knext.port_type(name="Browser Session", object_class=BrowserSessionObject, spec_class=MyPortObjectSpec) @knext.node(name="Create Browser Session", node_type=knext.NodeType.OTHER, icon_path="knime_selenium_extension/icons/icon.png", category=category) @knext.output_port(name="Browser Session", description="The output of your current browser session.", port_type=myPortType) class startWebinteraction(knext.PythonNode): """Short one-line description of the node. Long description of the node. Can be multiple lines. """ # Create instance of configuration classes browserSelectionC = browser_selection() browserConfig = browser_configuration() addArgs = add_args() def configure(self, configure_context): return def execute(self, exec_context) -> BrowserSessionObject: """if self.browserSelectionC.browserSelection == "Chrome": driver = chrome_browser() driver.get(self.browserConfig.urlSelection) elif self.browserSelectionC.browserSelection == "Edge": driver = edge_browser() driver.get(self.browserConfig.urlSelection) elif self.browserSelectionC.browserSelection == "Firefox": driver = firefox_browser() driver.get(self.browserConfig.urlSelection) """ driver = edge_browser() driver.get("https://www.google.de/?hl=de") myPortSpec = MyPortObjectSpec() browserSession = BrowserSessionObject(myPortSpec) #browserSession.from_connection_data(myPortSpec, driver) browserSession.to_connection_data() return browserSession