Execution stuck when calling undetected_chromedriver in Python - python

I'm trying to use the undetected_chromedriver.v2 package in Python Anaconda. My code goes like this:
import undetected_chromedriver.v2 as uc
ucoptions = uc.ChromeOptions()
print('1')
ucoptions.add_argument('--no-first-run')
print('2')
driver = uc.Chrome(chrome_options = ucoptions)
print('3')
In Anaconda Spyder, it works well if I use the F9 key to step into every single line of code and is able to open the Chrome browser normally as expected.
However, whenever I use the F5 key to run the whole file, it always get stuck in the driver = uc.Chrome(chrome_options = ucoptions) line and can only print out "1" and "2" on my screen.
Can anyone help me with this issue? Many thanks!

Related

Python Selenium: Quitting a browser instance outside of original function

I started learning python + selenium a few days ago, and am struggling to quit the browser outside of my bbdc_check function. My goal is whenever I interrupt bbdc_check or it encounters an error, I would like to quit the existing browser to start from scratch.
I keep encountering errors with quitting the browser. The error message for driver.quit() is "TypeError: quit() missing 1 required positional argument: 'self'".
I have a nagging suspicion that I'm supposed to use a class here, which I tried loosely off this solution, but still could not get it to work. Any ideas are appreciated, thank you.
FYI, date_a and date_b are not defined here because I deleted a bunch of code redundant to this issue. Assume that line of code works.
import selenium
from selenium import webdriver
import time
import sys
breakloop = 0
def bbdc_check():
global breakloop
driver = webdriver.Chrome(r'C:\<some dir>\chromedriver.exe')
driver.get('<a website>')
# A bunch of code here to compare 2 different dates
if (date_a < date_b):
breakloop = 1
else:
driver.quit()
time.sleep(600)
# The main while-loop to run the programme
while breakloop == 0:
try:
bbdc_check()
# If I manually interrupt, kill the programme
except KeyboardInterrupt:
driver = webdriver.Chrome
driver.quit()
sys.exit()
# If programme encounters error, try again from scratch
except:
driver = webdriver.Chrome
driver.quit()
time.sleep(30)
Seems you are creating new object of driver in each blocks as well as your function bbdc_check(). Create a single driver instance and use the same.

Pythons Webbrowser Module Will Never Open a Link in a new Window

I was trying to automate opening multiple user profiles given a list of names on a few different sites but i can not find a way to open a link in a new window meaning i can not sort the different sites i am opening into their own window collection.
here is my code:
import webbrowser
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
firefox_path="C:\\Program Files\\Mozilla Firefox\\Firefox.exe"
strURL = "http://www.python.org"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(chrome_path),1)
webbrowser.open(strURL, new=0)
webbrowser.open(strURL, new=1)
webbrowser.open(strURL, new=2)
webbrowser.get('chrome').open(strURL)
webbrowser.get('firefox').open(strURL)
webbrowser.get('chrome').open_new(strURL)
webbrowser.get('firefox').open_new(strURL)
no matter what value i put for new (0, 1, or 2), all that ever happens is it opens a new tab in the last window i clicked on. i have tried all of the other methods that i found in they python documentation for the webbrowser module and everyone online is just saying to use "new=1" or webbroswer.open_new() but neither of those work. and even when i point it at firefox it just goes to chrome.
P.S.
i found a small workaround that i am not totally satisfied with.
import webbrowser
chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
chrome_path_NW = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s --new-window"
firefox_path = "C:\\Program Files\\Mozilla Firefox\\Firefox.exe"
strURL = "http://www.python.org"
controller = webbrowser.get(chrome_path)
controllerNW = webbrowser.get(chrome_path_NW)
controllerNW.open(strURL, new=0)
controller.open(strURL, new=1)
controller.open(strURL, new=2)
controller.open("www.youtube.com", new=2)
the important thing to look at would be the "chrome_path" variable. i have changed it so it will run as a command and accept arguments. i found some launch arguments for chromium, here, that seem to work from chrome too. "--new-window" will open a new window and i can then open more tabs in that window but this is a total workaround of pythons module that i am not confident won't break if i am trying to use chrome while running this script. if there is any feature where i could group links together to open in specific windows that would be much more useful to me.
I realise this is a bit late but hopefully i can help someone in the future.
Basically you need to use the subprocess module to open up a new window before you load a new webpage
import subprocess
import time
import webbrowser
subprocess.Popen('open -a /Applications/Google\ Chrome.app --new', shell=True)
time.sleep(0.5) # this is to let the app open before you try to load a new page
webbrowser.open(url)

Why the selenium+python code can run line by line in terminal instead of in a whole file format?

import time,os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
target = "https://pan.baidu.com/"
driver.get(target)
name='name'
passwd='passwd'
driver.find_element_by_xpath('//div[#class="account-title"]/a').click()
driver.find_element_by_xpath('//input[#id="TANGRAM__PSP_4__userName"]').send_keys(name)
driver.find_element_by_xpath('//input[#id="TANGRAM__PSP_4__password"]').send_keys(passwd)
driver.find_element_by_xpath('//input[#id="TANGRAM__PSP_4__submit"]').click()
try:
path1 = driver.find_element_by_xpath('//a[#title="test1"]')
ActionChains(driver).move_to_element(path1).perform()
ActionChains(driver).context_click(path1).perform()
driver.find_element_by_xpath('//*[#class="context-menu"]/ul/li[21]').click()
time.sleep(5)
driver.find_element_by_xpath('//*[#class="g-button g-button-blue-large"]/span/span').click()
time.sleep(5)
except:
pass
try:
path2 = driver.find_element_by_xpath('//a[#title="test2"]')
ActionChains(driver).move_to_element(path2).perform()
ActionChains(driver).context_click(path2).perform()
driver.find_element_by_xpath('//*[#class="context-menu"]/ul/li[21]').click()
time.sleep(5)
driver.find_element_by_xpath('//*[#class="g-button g-button-blue-large"]/span/span').click()
time.sleep(5)
except:
pass
The above code snippet run successfully line by line in python3 terminal,
to input several lines everytime into python3 terminal,do three task for me.
1.automatically login to pan.baidu.com
2.to delete file named test1
3.to delete file named test2
Then to save the above code snippet as up.py and run it with time python3 up.py.
A very interesting thing happen.
1.running time
time python3 up.py
real 0m4.231s
user 0m0.192s
sys 0m0.080s
Why the running time is less then 20 seconds?
2.the third task haven't done
The file named test2 still there ,not deleted by program.
How to explain the strange issues and fix it?
To add a important line:
driver.implicitly_wait(20)

Python Selenium Geckodrive

Hey there just trying a basic browser launch using Firefox.
I've tried using executable path, if statement, and without an if statement and the browser will still not open. I've checked the shell and I don't have an error.My best guess is I'm missing an action of some sort I just need someone to point my in the right direction using my current code, thank you.
from selenium import webdriver
class testbot():
def botfox(self):
driver = self.driver = webdriver.firfox(geckodriver)
driver.get("https://wwww.google.com")
if __name__ == "__botfox__":
botfox()
ok, try this :)
from selenium import webdriver
class testbot():
def botfox(self):
self.driver = webdriver.Firefox()
self.driver.get("https://wwww.google.com")
if __name__ == '__main__':
testBotInstace = testbot()
testBotInstace.botfox()
I'd be surprised if that worked. Have you tried calling it via testbot().botfox() ?
webdriver.firfox would not work, as the syntax is webdriver.Firefox
webdriver.firfox(geckodriver) would not work as geckodriver is not defined anywhere
botfox() would not work because there is no function defined as that. There is one inside of testbot but you would need to first instantiate the class and then call it via testbot().botfox()

Do anyone know how to use python webbrowser.open under window

I had try with python with webbrowser.open, but it only work on IE. How to let it open chrome or firefox. I don't want it to open on IE, i wants to be open on Chrome or Firefox. Due to i try many method, but none of them works.
import time
import webbrowser
webbrowser.open('www.google.com')
you need specify your webbrowser's name, detal see webbrowser.get
import webbrowser
webbrowser.open('www.google.com')
a = webbrowser.get('firefox')
a.open('www.google.com') # True
UPDATE
If you have chrome or firefox installed in your computer, do as following:
chrome_path =r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe' # change to your chrome.exe path
# webbrowser is just call subprocess.Popen, so make sure this work in your cmd firstly
# C:\Users\Administrator>C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe www.google.com
# there two way solve your problem
# you have change \ to / in windows
# this seems a bug in browser = shlex.split(browser) in windows
# ['C:UsersAdministratorAppDataLocalGoogleChromeApplicationchrome.exe', '%s']
a = webbrowser.get(r'C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe %s')
a.open('www.google.com') #True
# or by register
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe'))
a = webbrowser.get('chrome')
a.open('www.google.com') #True
else you can try selenium, it provide much more functions and only need chromedriver.

Categories

Resources