2

I want the browser shoud't shut down when he can't find class for this i use try except block try-except is running but my browser is closing when try line come for execution but print line is executing in cmd

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import subprocess

# change as per requirement
email = "example@example.com"
password = "example"
MN = "mad max: fury road"

print("process Started")

with webdriver.Chrome(executable_path=r"C:\Users\lenovo\Downloads\bot\chromedriver.exe") as d:
    d.maximize_window()

    d.get("https://sflix.to/search/troll") 
    # locate the search bar element and enter the keyword
    time.sleep(5)
    d.find_element(By.CSS_SELECTOR, ".form-control.search-input").send_keys(MN) #entering keyword
    d.find_element(By.CSS_SELECTOR, ".form-control.search-input").send_keys(Keys.RETURN) #enter
    # locate the first search result link

try:
    elem = d.find_element(By.CLASS_NAME, "fd-btn").click()
except Exception as e:
    print(e)
    # add any further steps here
print("this will be print")

when try line come for execution my browser suddenly shut down with error message

DevTools listening on ws://127.0.0.1:56105/devtools/browser/86b384ff-9334-4e10-bd7c-fb96089e9bb3
[6664:6856:0127/223501.827:ERROR:device_event_log_impl.cc(215)] [22:35:01.827] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.
HTTPConnectionPool(host='localhost', port=56101): Max retries exceeded with url: /session/9ed569aae6e76e66149858a493add9ba/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001AE6C76F090>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))"

but my cmd paste this will be print

i tried nosuchelement instead of Exception as

try:
    elem = d.find_element(By.CLASS_NAME, "fd-btn").click()
    elem.click()
except NoSuchElementException:
    print("Element not found")

i check firewall too chrome was checked i changed the device but it didn't work i haven't tried changing browser because firfox gecko giving me error app can't run your pc same with edge

sound wave
  • 3,191
  • 3
  • 11
  • 29
jai mishra
  • 21
  • 4

0 Answers0