Issue
I'm using Selenium to auto-fill forms using forminfo.py
for the inputted information. (Right now I'm testing it out on Instagram just to figure everything out.) What I have done is, I have it check to see if the google chrome driver is already installed in a particular location and if not it installs it and if so it just continues and runs the code to fill out the form.
The problem I'm having is, everything runs fine IF the driver isn't installed and it installs it manually. BUT If the driver is already installed, after the form is filled out and it hits the submit button it closes the browser immediately. Ive only been learning Python for about a few weeks now and I cant figure out whats wrong. Also every where I've look I can find a solution to the problem. Also, I'm not getting any errors in my terminal.
################## IMPORTS ##################
import forminfo
import os
import urllib.request as urllib
import webbrowser
import zipfile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from time import sleep
################## CHROME DRIVER DOWNLOAD ##################
path = "C:/Users/"+os.getlogin()+"/Documents/chromedriver.exe"
isFile = os.path.isfile(path)
if not os.path.isfile(path):
webbrowser.get('windows-default').open("http://www.google.com", new=0)
zip_path, _ = urllib.urlretrieve(forminfo.downloadurl)
urllib.urlopen = zip_path
extract_dir = "C:/Users/"+os.getlogin()+"/Documents"
zipfile.ZipFile(zip_path, "r").extractall(extract_dir)
else:
print("Starting now....")
################## PROXY INFO ##################
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.socks_version = 5
prox.socks_proxy = forminfo.socks5ip
#prox.http_proxy = "ip_addr:port"
#prox.ssl_proxy = "ip_addr:port"
################## BROWSER ##################
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
#
capabilities['acceptInsecureCerts'] = True
capabilities['acceptSslCerts'] = True
#
driver_service = Service(executable_path="C:/Users/"+os.getlogin()+"/Documents/chromedriver")
chrome_options = Options()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("start-maximized")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
#
chrome_options.add_argument("--ignore-certificate-error")
chrome_options.add_argument("--ignore-ssl-errors")
#
driver = webdriver.Chrome(service=driver_service, options=chrome_options, desired_capabilities=capabilities)
driver.get('https://www.instagram.com')
################## BOT ##################
sleep(3)
elem = driver.find_element(By.NAME, "username")
elem.send_keys(forminfo.username)
elem = driver.find_element(By.NAME, "password")
elem.send_keys(forminfo.password)
sleep(3)
elem = driver.find_element(By.XPATH, "//*[@id=\"loginForm\"]/div/div[3]/button/div")
elem.click()
The only solution Ive found to the problem is here by adding a breakpoint()
at the very end of my code. But I feel like this would mess some things up down the road if I decided to expand on this code and eventually turn it into a .exe file with pyIntsaller or Nuitak. So I wanted to see If there was a solution before just moving forward with that resolution.
I know it's a problem with the ################## CHROME DRIVER DOWNLOAD ##################
code because I wasn't having this problem until I decided to add that code. This Is the very most recent part I added and everything was fine until then.
Solution
Converting comment to an answer.
You can use the detach
option to let the browser Window stay open after using it with Selenium, example:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
Answered By - user56700
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.