Python Selenium Geckodrive - python

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()

Related

Execution stuck when calling undetected_chromedriver in 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!

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.

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.

Selenium2Library code completion in PyCharm

I'm experimenting with creating a basic library extension for Robot Framework using Python, and I'm using PyCharm as the editor. For libraries imported directly code completion is working fine, but in this case I'm importing the Selenium2Library indirectly via a method:
def get_current_browser():
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
Which I call from other methods with something like
driver = get_current_browser()
This successfully grabs the webdriver browser instance from Robot Framework and lets me do as I please, but I don't get code hints when I go to edit a 'driver' variable. Is there way I can get hints in this scenario?
Here's the code in full:
from robot.libraries.BuiltIn import BuiltIn
from Selenium2Library.keywords.keywordgroup import KeywordGroup
import logging
def get_current_browser():
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
class MyLibrary(KeywordGroup):
def get_title_via_python(self):
driver = get_current_browser()
title = driver.title
logging.warn("checking title %s" % title)
return title
Try adding a docstring to your function to help PyCharm.
from selenium.webdriver import Remote # Remote imported only for code completion
def get_current_browser():
"""
:rtype: Remote
"""
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
More at http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html

Selenium-rc: How do you use CaptureNetworkTraffic in python

I've found many tutorials for selenium in java in which you first start selenium using s.start("captureNetworkTraffic=True"), but in python start() does not take any arguments.
How do you pass this argument? Or don't you need it in python?
I changed the start in selenium.py:
def start(self, captureNetworkTraffic=False):
l = [self.browserStartCommand, self.browserURL, self.extensionJs]
if captureNetworkTraffic:
l.append("captureNetworkTraffic=true")
result = self.get_string("getNewBrowserSession", l)
The you do:
sel = selenium.selenium('localhost', 4444, '*firefox', 'http://www.google.com')
sel.start(True)
sel.open('')
print sel.captureNetworkTraffic('json')
and it works like a charm
Start the browser in "proxy-injection mode" (note *pifirefox instead of *firefox). Then you can call the captureNetworkTraffic method.
import selenium
import time
sel=selenium.selenium("localhost",4444,"*pifirefox","http://www.google.com/webhp")
sel.start()
time.sleep(1)
print(sel.captureNetworkTraffic('json'))
I learned the *pifirefox "trick" here.

Categories

Resources