summaryrefslogtreecommitdiffstats
path: root/g4f/webdriver.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-03-12 02:06:06 +0100
committerGitHub <noreply@github.com>2024-03-12 02:06:06 +0100
commit6ef282de3a3245acbfecd08ae48dba85ff91d031 (patch)
tree0236c9678eea8f9c78ed7c09f3d86eaf3d7c691c /g4f/webdriver.py
parentUpdate .gitignore (diff)
downloadgpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar
gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.gz
gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.bz2
gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.lz
gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.xz
gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.zst
gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.zip
Diffstat (limited to '')
-rw-r--r--g4f/webdriver.py37
1 files changed, 28 insertions, 9 deletions
diff --git a/g4f/webdriver.py b/g4f/webdriver.py
index d9028a63..47962d96 100644
--- a/g4f/webdriver.py
+++ b/g4f/webdriver.py
@@ -2,9 +2,9 @@ from __future__ import annotations
try:
from platformdirs import user_config_dir
+ from undetected_chromedriver import Chrome, ChromeOptions
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
- from undetected_chromedriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
@@ -12,10 +12,10 @@ try:
from selenium.common.exceptions import NoSuchElementException
has_requirements = True
except ImportError:
- from typing import Type as WebDriver
+ from typing import Type as WebDriver, Callable as user_config_dir
has_requirements = False
-import time
+import time
from shutil import which
from os import path
from os import access, R_OK
@@ -29,6 +29,24 @@ try:
except ImportError:
has_pyvirtualdisplay = False
+try:
+ from undetected_chromedriver import Chrome as _Chrome, ChromeOptions
+ from seleniumwire.webdriver import InspectRequestsMixin, DriverCommonMixin
+
+ class Chrome(InspectRequestsMixin, DriverCommonMixin, _Chrome):
+ def __init__(self, *args, options=None, seleniumwire_options={}, **kwargs):
+ if options is None:
+ options = ChromeOptions()
+ options.add_argument('--proxy-bypass-list=<-loopback>')
+ config = self._setup_backend(seleniumwire_options)
+ options.add_argument(f"--proxy-server={config['proxy']['httpProxy']}")
+ options.add_argument("--ignore-certificate-errors")
+ super().__init__(*args, options=options, **kwargs)
+ has_seleniumwire = True
+except:
+ from undetected_chromedriver import Chrome, ChromeOptions
+ has_seleniumwire = False
+
def get_browser(
user_data_dir: str = None,
headless: bool = False,
@@ -106,7 +124,7 @@ def bypass_cloudflare(driver: WebDriver, url: str, timeout: int) -> None:
}});
""", element, url)
element.click()
- time.sleep(3)
+ time.sleep(5)
# Switch to the new tab and close the old tab
original_window = driver.current_window_handle
@@ -126,9 +144,10 @@ def bypass_cloudflare(driver: WebDriver, url: str, timeout: int) -> None:
...
except Exception as e:
if debug.logging:
- print(f"Error bypassing Cloudflare: {e}")
- finally:
- driver.switch_to.default_content()
+ print(f"Error bypassing Cloudflare: {str(e).splitlines()[0]}")
+ #driver.switch_to.default_content()
+ driver.switch_to.window(window_handle)
+ driver.execute_script("document.href = document.href;")
WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "body:not(.no-js)"))
)
@@ -223,13 +242,13 @@ class WebDriverSession:
self.default_driver.close()
except Exception as e:
if debug.logging:
- print(f"Error closing WebDriver: {e}")
+ print(f"Error closing WebDriver: {str(e).splitlines()[0]}")
finally:
self.default_driver.quit()
if self.virtual_display:
self.virtual_display.stop()
def element_send_text(element: WebElement, text: str) -> None:
- script = "arguments[0].innerText = arguments[1]"
+ script = "arguments[0].innerText = arguments[1];"
element.parent.execute_script(script, element, text)
element.send_keys(Keys.ENTER) \ No newline at end of file