I am trying to run a script which is supposed to open several website to search in using selenium and chrom
For each site in the array it will open a new web tab and run a search in this site.
I either modify the web address to include the search GET request or search for the “Search” filed, enter the search text and hit “Enter” or press the correct button.
The problem I encounter is that when it fails to handle a site – it opens a new tab for the site and fails it either for these lines:
driver.execute_script(f"window.open('{site_url}', '_blank');") driver.switch_to.window(driver.window_handles[-1])
When this happens it keeps failing to open the rest of the sites and report:
print("Connection timed out! Skipping", site_name)
For the rest of the sites.
What I want to achieve, is that when it fails to handle a site it will skip it and continue to the next site and open a new tab for it.
I tried to add try/except blocks to handle this problem, however, it appears the connection to the browser is lost or corrupt after the first error.
I believe the relevant code is this:
selected_sites = [key for key, var in self.checkbuttons.items() if var.get()]
entered_text = self.text_entry.get()
if entered_text == "" :
messagebox.showerror("Empty search", "Search fiels if empty.")
return
chrome_options = Options()
driver = webdriver.Chrome(options=chrome_options) # Ensure you have the ChromeDriver executable
driver.command_executor.set_timeout(10)
if_first = True
for site_key in selected_sites:
site_info = self.sites[site_key]
site_name, site_url = site_info["name"], site_info["url"]
cont=True
if "SEARCH_FOR" in site_url:
site_url = site_url.replace("SEARCH_FOR", entered_text)
if self.debug:
print(f"replacing SEARCH_FOR with {entered_text}")
# Open a new tab
try:
if if_first:
if_first=False
if self.debug:
print(f" => driver.get{site_url}")
driver.get(site_url)
else:
if self.debug:
print(f" => window.open('{site_url}', '_blank');")
try:
driver.execute_script(f"window.open('{site_url}', '_blank');")
except:
print(f"Failed to execute script for {site_name}. Skipping...")
continue
if self.debug:
print(f" => Window handles: {driver.window_handles}")
try:
driver.switch_to.window(driver.window_handles[-1])
except:
print(f"Failed to switch to new tab for {site_name}. Skipping...")
continue # Move to the next site
if self.debug:
print(f"Successfully opened: {site_name}")
except:
print("Connection timed out! Skipping", site_name)
continue
if site_info.get('press_before_id'):