Selenium code in Python produces syntax errors - python

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

Related

Scheduled trigger of chrome extension app

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.

Script can't find Element but Console can [Selenium] [Python]

I'm trying to write script to automate the process of downloading the instagram stories but I'm failing already when trying to log in.
I'm writing the code inside Pycharm. I just tried my usual approach to any problem. First, solve it with typing the commands out in the console and if it works writing the commands which worked inside the console down in a script. But here is the issue. The function which worked perfectly fine inside the python console fails inside the script.
I've noticed that my selenium was outdated but upgrading it didn't help ether. I also made a new project to test weather that made difference, which it didn't.
I've also tried skipping the first step inside the script and just opening the url to which I'm redirected. But the second commands failed as well.
When I create a new variable to store the output of the driver.find_element_by_link_text() in, it returns an empty list. This leads me to belive that somehow selenium is unable to search the contetns of the page.
I've also tried the same on Chrome and Safari. This also didn't work.
Here is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://instagram.com/")
#next command fails
driver.find_element_by_link_text("Melde dich an.").click()
#if the first command is skipped by entering in the url
#in driver.get(https://www.instagram.com/accounts/login/?source=auth_switcher)
#the following command fails as well.
driver.find_element_by_name("username").send_keys("HereIsTheUsername")
driver.find_element_by_name("password").send_keys("HereIsThePassword")
driver.find_element_by_name("password").send_keys(Keys.RETURN)
driver.close()
In the console these commands worked as mentioned,
Here is what I've entered into the console:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://instagram.com/")
driver.find_element_by_link_text("Melde dich an.").click()
#if it failed here would be an error message
element = driver.find_element_by_name("username")
With the script the error message is this:
Traceback (most recent call last): File
"/Users/alisot2000/PycharmProjects/Instagram downloader/venv/Main.py",
line 6, in
driver.find_element_by_link_text("Melde dich an.").click() File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text) File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 978, in find_element
'value': value})['value'] File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 321, in execute
self.error_handler.check_response(response) File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: Melde dich an.
Issues you might be experiencing:
1. Synchronization Issue
For most automation tasks, there will be different loading times of web pages based on the processing power of the machine and how strong your internet connection is.
To solve this there are library import Waits from selenium that we can use.
Here is a sample below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
2. Wrong language set in selenium profile
Selenium will use your locale in most cases when running automation scripts but in the case that you might want another language here is a sample code for FireFox.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
# switch out 'de' with another two character language code
profile.set_preference("intl.accept_languages",'de')
driver = webdriver.Firefox(firefox_profile=profile, executable_path='<insert_your_gecko_driver_path_here>')
driver.get("https://instagram.com/")
driver.close()
3. Working Code(Tested on Mojave 10.14.5)
Here is a diff of your code and the altered code: https://www.diffchecker.com/G0WWB4Ry
setup a virtualenv
pip install selenium
download geckodriver
set path to gecko driver in code
run script with success result
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# these two imports are for setting up firefox driver and options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# import these three lines below if you are having synchronization issues
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
profile = webdriver.FirefoxProfile()
# here is where you need to set your language explicitly if its defaulting to an undesired language
# just replace the second parameter with your 2 character language code
# this line is not needed if your desired language is locale
profile.set_preference("intl.accept_languages",'de')
# throw in your path here <insert_your_gecko_driver_path_here>
driver = webdriver.Firefox(firefox_profile=profile, executable_path='<insert_your_gecko_driver_path_here>')
driver.get("https://instagram.com/")
# added these two lines below to solve synchronization issue
# element wasnt clickable until page finished loading
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Melde dich an.")))
#next command fails
driver.find_element_by_link_text("Melde dich an.").click()
#if the first command is skipped by entering in the url
#in driver.get(https://www.instagram.com/accounts/login/?source=auth_switcher)
#the following command fails as well.
driver.find_element_by_name("username").send_keys("HereIsTheUsername")
driver.find_element_by_name("password").send_keys("HereIsThePassword")
driver.find_element_by_name("password").send_keys(Keys.RETURN)
driver.close()
def ClickElementByName(name,driver):
while True:
try:
driver.find_element_by_name(name).click()
break
except:
sleep(1)
pass
Too long to wait the website run.
Replace ClickElementByName("username", driver)
driver.find_element_by_xpath('//input[#name="username"]').send_keys("HereIsTheUsername")
driver.find_element_by_xpath('//input[#name="password"]').send_keys("HereIsTheUsername")
driver.find_element_by_xpath('//div[text()="Log In"]').click()

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

TypeError: get() missing 1 required positional argument: 'url' error using GeckoDriver and Firefox through Selenium and Python

Executing below code in pycharm.
from selenium import webdriver
browser = webdriver.Firefox
browser.get('https://www.google.com')
Error:
TypeError: get() missing 1 required positional argument: 'url'
How can I solve the error?
Specify the path the chrome driver is located in for example when calling
webdriver.Firefox(‘C://Users/Username/Downloads/‘)
This worked for me:
from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
Alternate code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
In my case, I got this error for not using parenthesis ().
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.google.com')
Try with braces while creating Firefox instance. see below example.
from selenium import webdriver
browser = webdriver.Firefox() #focus on () at the end
browser.get('https://www.google.com')
The constructor is driver = webdriver.Firefox(). So in your code block you need to replace driver = webdriver.Firefox with:
driver = webdriver.Firefox()
Additionally, you may need to pass the absolute path of the GeckoDriver binary as follows:
driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
The issue is caused bacause there are not,()pharentesis, put the pharantesis end to the line
Check this
In Selenium and python
driver = webdriver.Chrome()

TypeError: 'module' object is not callable ( when importing selenium )

i have problem when running this code :
>>> from selenium import webdriver
>>> driver = webdriver.firefox()
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
driver = webdriver.firefox()
TypeError: 'module' object is not callable
i have searched for the problem and i got some results. but unfortunately , they didn't work. So , how can i solve this?
thanks.
You have made a typo.
webdriver.Firefox()
Note the capital F.
the same goes for other browsers!
e.g.
webdriver.chrome Vs. webdriver.Chrome
(its even harder to notice this!)
thanks so much for the help! ;)
Another way is:
from selenium.webdriver import Chrome.
driver = Chrome()
When typing "Chrome" Note the capital C.
You probably gonna need to specify the executable_path for chromedriver.exe:
driver = Chrome(executable_path="path_in_here")
This error message...
TypeError: 'module' object is not callable
......implies that your program is trying to call a python module.
You need a minor modification in the offending line of code. You have used:
driver = webdriver.firefox()
Where as firefox is a module for example as in:
selenium.webdriver.firefox.options
So you have to change firefox() to Firefox() and your effective line of code will be:
driver = webdriver.Firefox()
Likewise:
For Chrome:
driver = webdriver.Chrome()
For Internet Explorer:
driver = webdriver.Ie()

Categories