im trying to make a program that works like this but i keep getting this error
Traceback (most recent call last):
File "/workspaces/vscode-remote-try-python/VirtualBrowser/main.py", line 7, in <module>
driver = webdriver.Chrome(service=r'VirtualBrowser/chromedriver')
File "/home/vscode/.local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
super().__init__(
File "/home/vscode/.local/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 101, in __init__
self.service.start()
AttributeError: 'str' object has no attribute 'start'
this is my code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromeOptions = webdriver.ChromeOptions()
chromeOptions.binary_location = "/workspaces/vscode-remote-try-python/VirtualBrowser/chromedriver"
chromeDriver = 'VirtualBrowser/chromedriver'
driver = webdriver.Chrome(service=chromeDriver)
driver.get("https://google.com")
input("Running...")
im not exactly the greatest at debugging and most of the code is fixes that ive tried and they have worked towards throwing less errors. any help would be appreciated, thanks
I think there is a problem in driver = webdriver.Chrome(service=chromeDriver) where service kwarg should be a service object.
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# ...
chrome_options = Options()
chrome_service = Service("path")
driver = webdriver.Chrome(options=chrome_options, service=chrome_service)
Related
I tried to add chrome extension when open chrome webdriver in python.
OK.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
extension_path = r'C:/Users/path/to/extension'
chrome_options = Options()
chrome_options.add_argument('load-extension=' + extension_path)
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-infobars")
browser = webdriver.Chrome(executable_path=r'C:/Users/path/to/chromedriver.exe', options = chrome_options)
The codes runs fine and the extension was presented. However, when I split into 2 files and use classes.
variables.py
from selenium.webdriver.chrome.options import Options
driver_path = r'C:/Users/path/to/chromedriver.exe'
extension_path = r'C:/Users/path/to/extension'
chrome_options = Options()
chrome_options.add_argument('load-extension=' + extension_path)
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-infobars")
broken.py
import variables as vb
from selenium import webdriver
class nokair(webdriver.Chrome):
def __init__(self, executable_path = vb.driver_path, options = vb.chrome_options):
self.driver_path = executable_path
self.options = options
super().__init__()
init = nokair()
When launched, the webdriver was executed without any extension. I've tried other workaround
import variables as vb
from selenium import webdriver
class nokair(webdriver.Chrome):
def __init__(self, executable_path = vb.driver_path, options = vb.chrome_options):
self.driver_path = executable_path
self.options = options
super().__init__(executable_path, options)
init = nokair()
This threw an error. What did I do wrong?
Traceback (most recent call last):
File "c:\Users\Kittinun\Desktop\VS workspace\Playground\Flight\nokair.py", line 93, in <module>
nok = nokair()
File "c:\Users\Kittinun\Desktop\VS workspace\Playground\Flight\nokair.py", line 14, in __init__
super(nokair, self).__init__(executable_path, options)
File "C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
cmd.extend(self.command_line_args())
File "C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\chrome\service.py", line 45, in command_line_args
return ["--port=%d" % self.port] + self.service_args
TypeError: %d format: a number is required, not Options
broken.py does not work because there is not options field in webdriver.Chrome, hence it is not used by that driver.
The latter case does not work because you are not using named parameters which forces Python to assign them sequentially. The second parameter of the constructor is port. But you pass Options to it.
Workable code would be
import variables as vb
from selenium import webdriver
class nokair(webdriver.Chrome):
def __init__(self):
super().__init__(executable_path=vb.driver_path, options=vb.chrome_options)
init = nokair()
In my example I left nokair constructor without any parameter to simplify the things and not mess up parameter names.
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
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
driver.get("https://twitter.com/login?lang=en")
When I try to run my code, I get this error:
Warning (from warnings module):
File "/Users/toprak/Desktop/topla.py", line 19
driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
DeprecationWarning: use options instead of firefox_options
Traceback (most recent call last):
File "/Users/toprak/Desktop/topla.py", line 19, in <module>
driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 137, in __init__
if options.binary is not None:
AttributeError: 'Options' object has no attribute 'binary'
When I delete the lines which are about options and take out "firefox_options=options", the code works fine. What should I do to fix this?
Instead of using firefox_options object you need to use options object. Additionally you need to use the headless attribute. So your effective code block will be:
options = FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(executable_path='/Users/toprak/Desktop/geckodriver', options=options)
driver.get("https://twitter.com/login?lang=en")
References
You can find a couple of relevant detailed discussions in:
How to make Firefox headless programmatically in Selenium with Python?
The --headless argument works fine in Firefox (geckodriver) these days.
If you're getting the error mentioned in the title, then you're probably accidentally creating or passing a Chrome-based Options object rather than a Firefox-based Options object.
To avoid that mistake, it's best to create an import alias for both of them so that they're easier to distinguish.
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
chrome_options = ChromeOptions()
chrome_options.add_argument('--headless')
chrome_driver = webdriver.Chrome(executable_path = r"..\mypath\chromedriver.exe", options=chrome_options)
firefox_options = FirefoxOptions()
firefox_options.add_argument('--headless')
firefox_driver = webdriver.Firefox(executable_path = r"..\mypath\geckodriver.exe", options=firefox_options)
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
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()