0

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")
nvs0000
  • 107
  • 1
  • 5

2 Answers2

0
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 *
RafalS
  • 5,834
  • 1
  • 20
  • 25
  • Thanks @Rafals , did your last bit of answer get cut off? Was I suppose to do something with `Common_Methods.GenericMethods import *`? I followed the instruction and my program returned this: File "chromeClick_v1.py", line 3, in from Common_Methods.GenericMethods import * ModuleNotFoundError: No module named 'Common_Methods' – nvs0000 May 06 '20 at 07:46
  • Common_Methods souds like some local python utils file next to your script – RafalS May 06 '20 at 08:05
  • Nice, did that. now faced with issues with 'chrome_options' -- ....rubicon/objc/ctypes_patch.py:21: UserWarning: rubicon.objc.ctypes_patch has only been tested with Python 3.4 through 3.7. You are using Python 3.8.1. Most likely things will work properly, but you may experience crashes if Python's internals have changed significantly. warnings.warn( Traceback (most recent call last): File "chromeClick_v1.py", line 20, in driver = webdriver.Chrome(executable_path=os.path.join(chrome_options=options)) TypeError: join() got an unexpected keyword argument 'chrome_options' – nvs0000 May 06 '20 at 09:22
0

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.

nvs0000
  • 107
  • 1
  • 5