Scheduled trigger of chrome extension app - python

I'm trying to find a way to implement:
- this code using python
- schedule a timed trigger
- select a tab on the google chrome
I've copied and pasted the solution into my python just to test it but seem to be getting a URL error below, can someone help me understand why a variable is facing a syntax error?
Here is my code:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from Common_Methods.GenericMethods import *
import pyautogui #<== need this to click on extension
options = ChromeOptions()
#from stack overflow(https://stackoverflow.com/questions/53172127/click-on-elements-in-chrome-extension-with-selenium?rq=1):
#options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
options.add_argument("--load-extension=" + r"/Users/erikwayne/Library/Application Support/Google/Chrome/Profile 2/Extensions/fdpohaocaechififmbbbbbknoalclacl/6.5_0") #<== loading unpacked extension
driver = webdriver.Chrome(
executable_path=os.path.join(chrome_options=options)
url = "https://www.google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(GenericMethods.get_full_path_to_folder('autogui_ref_snaps') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")
Here is my error message:
MBP:Testing chrome extension erikwayne$ python3 chromeClick_v1.py
File "chromeClick_v1.py", line 15
url = "https://www.google.com/"
^
SyntaxError: invalid syntax
Expanded debugging:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pdb.py", line 1703, in main
pdb._runscript(mainpyfile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pdb.py", line 1572, in _runscript
self.run(statement)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/bdb.py", line 587, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "/Users/erikwayne/Downloads/Testing chrome extension/chromeClick_v1.py", line 11
url = "https://www.google.com/"
^
SyntaxError: invalid syntax
*Edit: Tried the modified code below from #RafalS, but had more error.
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from Common_Methods.GenericMethods import *
import pyautogui #<== need this to click on extension
import os
options = ChromeOptions()
#from stack overflow(https://stackoverflow.com/questions/53172127/click-on-elements-in-chrome-extension-with-selenium?rq=1):
#options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
options.add_argument("--load-extension=" + r"/Users/erikwayne/Library/Application Support/Google/Chrome/Profile 2/Extensions/fdpohaocaechififmbbbbbknoalclacl/6.5_0") #<== loading unpacked extension
driver = webdriver.Chrome(executable_path=os.path.join(chrome_options=options))
url = "https://www.google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(get_full_path_to_folder('downloads') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")

driver = webdriver.Chrome(
close the parentheses
driver = webdriver.Chrome()
You'll also need to import os and replace
GenericMethods.get_full_path_to_folder
with
get_full_path_to_folder
since you did a star import:
from Common_Methods.GenericMethods import *

I did it and sorted it out by installing the chrome webdriver to a more accessible folder path, ignoring ospaths or GenericMethods altogether.
As for the driver = webdriver.Chrome issue, the original code was missing a comma between arguments, so I just filled in in.
driver = webdriver.Chrome(executable_path=os.path.join(r'/Users/USERNAME/Downloads/Testing chrome extension/chromedriver/chromedriver'), options=options)
It's hacked together but it works.

Related

why I get __init__.py problems when using selenium webdrivers chrome?

im working on a project and have this as code:
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
print(driver.current_url)
but i get an error and i dont know how to fix it, well mutle tracebacks to error i think, here is the error:
Traceback (most recent call last):
File "C:\Users\ytty\PycharmProjects\test hack\main.py", line 2, in <module>
from selenium import webdriver
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 36, in <module>
from .service import Service
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\service.py", line 21, in <module>
class Service(service.Service):
AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'
Process finished with exit code 1
can anyone help?
I do it this way:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=ChromeDriverManager().install())
This way it will automatically download the optimal ChromeDriver version on your cache and use it every time you need. Also the experimental part helps to avoid some error that might appear due to driver bugs

Selenium code in Python produces syntax errors

I am getting syntax errors. I tried this first:
from selenium import webdriver
driver = webdriver.Chrome(C:\Webdriver)
driver.get('http://www.google.com')
And got this:
File "C:/Users/donbr/PycharmProjects/Seleniumtes/Selenium Test.py", line 3
driver = webdriver.Chrome(C:\Webdriver)
^
SyntaxError: invalid syntax
So I changed it to this because I thought it was asking me to take the C: out
from selenium import webdriver
driver = webdriver.Chrome(\Webdriver)
driver.get('http://www.google.com')
just for it to say this:
File "C:/Users/donbr/PycharmProjects/Seleniumtes/Selenium Test.py", line 3
driver = webdriver.Chrome(\Webdriver)
^
SyntaxError: unexpected character after line continuation character
What does that mean? Why is it pointing to the bracket like it isn't supposed to be there? How is that an unexpected character?
You would need to pass the complete path to chrome web driver as shown below.
from selenium import webdriver
driver = webdriver.Chrome(<complete path to chrome web driver executable>)
driver.get('http://www.google.com')

Unable to open site url in browser

I have created simple basic automation script in Python using Selenium..
Getting unwanted exception.
File:-
import pandas as pd
from pandas import ExcelWriter
from selenium import webdriver
import selenium as sel
# Data = pd.read_excel(r"C:\Users\Admin\PycharmProjects\Web_Automation_Form_Filling\challenge.xlsx",sheet_name="Sheet1")
# browser = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
browser.sleep(1000);
browser.get("http://www.python.org")
Error log:-
C:\Users\Admin\PycharmProjects\Web_Automation_Form_Filling\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/Web_Automation_Form_Filling/venv/Web_Auto_Filling.py
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/Web_Automation_Form_Filling/venv/Web_Auto_Filling.py", line 10, in <module>
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
self.assert_process_still_running()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 109, in assert_process_still_running
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0
Process finished with exit code 1
Any suggestion will be appreciated..
Thanks...
instead of chrome application try providing the chrome driver instead
more information on the site : https://sites.google.com/a/chromium.org/chromedriver/getting-started
Sample code :
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
Download the ChromeDriver binary for your platform under the downloads section of this site
reference link to download : chrome driver
This code should work (better to use firefox for selenium):
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
# noinspection PyUnresolvedReferences
import wget
DesiredCapabilities.PHANTOMJS[
"phantomjs.page.settings.userAgent"
] = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0"
if browser == "firefox":
driver = webdriver.Firefox()
else:
driver = webdriver.PhantomJS(
CFG_phantomjs
) # r"D:/_devs/webserver/phantomjs-1.9.8/phantomjs.exe"
driver.get("https://tourwebsite")
username = driver.find_element_by_id("login_field")
password = driver.find_element_by_id("password")
username.clear()
The problem here in your codes is that you are passing chrome executable path rather than passing the path to chromedriver which is a different executable.
An appropriate version of chromedriver can be downloaded from here according to your Chrome version.
For more info, you can refer to the chromedriver documentation here.
And your final code should be something like:
from selenium import webdriver
path = 'C:/Users/Avinash/Downloads/chromedriver.exe'
driver = webdriver.Chrome(path)
driver.get('http://www.google.com/');
#..here what ever you want to do with page here
driver.quit()

seleniumwire_options=None: SyntaxError: invalid syntax

I am using selenium-wire to click a button using Chrome browser. Up until today, my code worked fine. However, now I get the following error and I am not sure why:
Traceback (most recent call last):
File "Scraping_fx.py", line 1, in <module>
from seleniumwire import webdriver
File "C:\Users\me\Anaconda3\envs\project\lib\site-
packages\seleniumwire\webdriver\__init__.py", line 3, in <module>
from .browser import Chrome, Edge, Firefox, Safari # noqa
File "C:\Users\me\Anaconda3\envs\project\lib\site-
packages\seleniumwire\webdriver\browser.py", line 14
def __init__(self, *args, seleniumwire_options=None, **kwargs):
^
SyntaxError: invalid syntax
I am using python 2.7.16; selenium-wire 1.0.4.
from seleniumwire import webdriver
from selenium.common.exceptions import ElementClickInterceptedException,NoSuchElementException
import logging
def scrape_website(url):
# Configure browser driver
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
#Instantiate driver and navigate to URL
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(30)
driver.get(url)
Am I missing something here?
Can't see anything wrong with your code :)
But selenium-wire 1.0.4 requires Python 3.4+.
You can find all the info on pypi.org - here

python selenium not able to open Firefox browser

I am using Python with selenium. I am getting following error when I am trying to open Firefox using python selenium
>>> driver = webdriver.Firefox()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 59, in __init__
self.binary, timeout),
File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\firefox\extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\firefox\firefox_binary.py", line 61, in launch_browser
self._wait_until_connectable()
File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\firefox\firefox_binary.py", line 105, in _wait_until_connectable
self.profile.path, self._get_firefox_output()))
selenium.common.exceptions.WebDriverException: Message: 'Can\'t load the profile. Profile Dir: c:\\users\\workshop\\appdata\\local\\temp\\tmpzcofay Firefox output: 1422716384881\taddons.manager\tDEBUG
\tLoaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]\r\n1422716384882\taddons.manager\tDEBUG\tLoaded provider scope for resource://gre/modules/LightweightThemeMan
ager.jsm: ["LightweightThemeManager"]\r\n1422716384884\taddons.xpi\tDEBUG\tstartup\r\n1422716384886\taddons.xpi\tINFO\tMapping fxdriver#googlecode.com to c:\\users\\workshop\\appdata\\local\\temp\\tmp
zcofay\\extensions\\fxdriver#googlecode.com\r\n1422716384887\taddons.xpi\tINFO\tMapping {F17C1572-C9EC-4e5c-A542-D05CBB5C5A08} to C:\\Program Files (x86)\\DAP\\DAPFireFox\r\n1422716384887\taddons.xpi\
tINFO\tMapping {82AF8DCA-6DE9-405D-BD5E-43525BDAD38A} to C:\\Program Files (x86)\\Mozilla Firefox\\browser\\extensions\\{82AF8DCA-6DE9-405D-BD5E-43525BDAD38A}.xpi\r\n1422716384888\taddons.xpi\tINFO\tM
apping {972ce4c6-7e08-4474-a285-3208198ce6fd} to C:\\Program Files (x86)\\Mozilla Firefox\\browser\\extensions\\{972ce4c6-7e08-4474-a285-3208198ce6fd}\r\n1422716384888\taddons.xpi\tDEBUG\tSkipping una
vailable install location app-system-share\r\n1422716384888\taddons.xpi\tDEBUG\tSkipping unavailable install location app-system-local\r\n1422716384889\taddons.xpi\tINFO\tMapping {74c841e3-b59f-479e-8
d7a-e26a942a87c8} to C:\\Program Files (x86)\\WinZip Courier\\FFExt\r\n1422716384889\taddons.xpi\tINFO\tMapping {01A8CA0A-4C96-465b-A49B-65C46FAD54F9} to C:\\Program Files (x86)\\Adobe\\Adobe Contribu
te CS5.1\\Plugins\\FirefoxPlugin\\{01A8CA0A-4C96-465b-A49B-65C46FAD54F9}\r\n1422716384889\taddons.xpi\tINFO\tMapping web2pdfextension#web2pdf.adobedotcom to C:\\Program Files (x86)\\Adobe\\Acrobat 10.
0\\Acrobat\\Browser\\WCFirefoxExtn\r\n1422716384889\taddons.xpi\tINFO\tMapping daplinkchecker#speedbit.com to C:\\Program Files (x86)\\DAP\\daplinkchecker\r\n1422716384890\taddons.xpi\tINFO\tMapping s
earchpredict#speedbit.com to C:\\Program Files (x86)\\SearchPredict\\PRFireFox\r\n1422716384890\taddons.xpi\tINFO\tMapping {0329E7D6-6F54-462D-93F6-F5C3118BADF2} to C:\\Program Files (x86)\\SPEEDbit V
ideo Downloader\\SPFireFox\r\n1422716384890\taddons.xpi\tINFO\tMapping adapter#gingersoftware.com to C:\\Program Files (x86)\\Ginger\\Mozilla\\adapter#gingersoftware.com\r\n1422716384891\taddons.xpi\t
DEBUG\tcheckForChanges\r\n'
>>> driver = webdriver.Firefox()
You need to provide path:
from selenium import webdriver
ff_profile = webdriver.FirefoxProfile()
browser = webdriver.Firefox(firefox_profile=ff_profile)
Please check if it works for You (it does for me, whereas without path i get same error as You do).
Your selenium version is quite old.
You have to install the latest version 2.53, it should solve your problem.
Remember to introduce selenium and firefox in your system path then you can use this code, some times depend of the Firefox version that you have you will need install the gekodriver too
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
driver = webdriver.Firefox()
driver.get("YOUR_URL")
If you didn't introduce them in the system path you can use this other code:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("C:\Program Files (x86)\Mozilla Firefox\Firefox.exe")#path of Firefox.exe
driver = webdriver.Firefox(firefox_binary=binary)
driver.get("YOUR_URL")
You need to import the correct modules. Don't forget to call them. Check my example below:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("Insert URL here")
Run that and it should work. Let me know if it doesn't.

Categories