Refreshing a webpage - python

I am trying to make a function that refreshes any page when given the URL. But the program won't run. What am I doing wrong? This is my code:
from selenium import webdriver
import time
def page_refresh(url):
driver = webdriver.Firefox()
driver.get(url)
x = 0
while x <= 5:
time.localtime(10)
driver.refresh(url)
driver.close
page_refresh('https://www.wikipedia.org/')
This is what I get:
Traceback (most recent call last):
File "C:\Users\100453649\PycharmProjects\AutoRefresher\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\100453649\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\100453649\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/100453649/PycharmProjects/AutoRefresher/Main.py", line 13, in <module>
page_refresh('https://www.wikipedia.org/')
File "C:/Users/100453649/PycharmProjects/AutoRefresher/Main.py", line 5, in page_refresh
driver = webdriver.Firefox()
File "C:\Users\100453649\PycharmProjects\AutoRefresher\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
self.service.start()
File "C:\Users\100453649\PycharmProjects\AutoRefresher\venv\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Your Exception clearly says that
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Solution:
Add gecko driver path:
Example:
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get(url)

Related

Trying to open a webpage using selenium but getting error

I wrote this simple code:
from selenium import webdriver
webdriver.Chrome().get('www.google.co.in')
but I'm getting this error:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\WebAutomation\venv\Automation.py", line 2, in <module>
webdriver.Chrome().get('www.google.co.in')
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__
self.service.start()
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
Someone please help me. I'm a beginner, please explain/guide in detail (I don't know complex concepts)
You will need additional the Selenium-Chrome-Driver.exe!
The last line of errors tells this to you.
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
The driver itself will be started from your programm and will hold the binding between your program and the browser chrome itself.
The driver.exe can be easily placed in the programm-startup-path (I recommend this for the first steps) or placed where ever you want, but than you must define the special variable of the PATH to the driver.exe.
You can get the driver.exe behind this link:
Main page:
https://chromedriver.chromium.org/getting-started
Download Page:
https://chromedriver.chromium.org/downloads
Please ensure, that your Browser-Version match to the driver-Version.

Using Selenium webdriver.Chrome results in PermissionError: [WinError 5] Access is denied [duplicate]

This question already has answers here:
'Webdrivers' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
(22 answers)
Closed 2 years ago.
I'm trying to web scrape reviews from (https://boxes.mysubscriptionaddiction.com/box/boxycharm?ratings=true#review-update-create) but when I run the code:
from selenium import webdriver
chrome_path = r"C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\Scripts"
driver = webdriver.Chrome(chrome_path)
driver.get("https://boxes.mysubscriptionaddiction.com/box/boxycharm?ratings=true#review-update-create")
review = driver.find_element_by_class_name("comment-body")
for post in review:
print(post.text)
I got the following error(s). What do I need to do to fix this?
"C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\python.exe" "C:/Users/Sara Jitkresorn/PycharmProjects/untitled/venv/SubsAddict.py"
Traceback (most recent call last):
File "C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Sara Jitkresorn/PycharmProjects/untitled/venv/SubsAddict.py", line 3, in <module>
driver = webdriver.Chrome(chrome_path)
File "C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Sara Jitkresorn\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'Scripts' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
You should replace all "\" with "/".
Your chrome path is wrong, you need to find path to chrome executable, it should be stored in Program Files or Program Files x86 Google subfolder.

Selenium: Launch Firefox with Extensions Enabled

I need to launch Firefox with Browsec add-on. I've written some code, but get an error:
from selenium import webdriver
url = input("Enter url to scrape from: ")
profile = webdriver.FirefoxProfile()
profile.add_extension('/home/myusername/.mozilla/firefox/b5qyukpg.default/extensions/browsec#browsec.com.xpi')
browser = webdriver.Firefox()
browser.get(url)
What i get is:
Traceback (most recent call last):
File "scraper.py", line 7, in <module>
profile.add_extension('/home/myusername/.mozilla/firefox/b5qyukpg.default/extensions/browsec#browsec.com.xpi')
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 95, in add_extension
self._install_extension(extension)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 274, in _install_extension
addon_details = self._addon_details(addon)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 351, in _addon_details
raise AddonFormatError(str(e), sys.exc_info()[2])
selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno 2] No such file or directory: '/tmp/tmpd7nyxubj.browsec#browsec.com.xpi/install.rdf'", <traceback object at 0x7f23f786eb08>)
How can i fix that error?

errors with webdriver.Firefox() with selenium

I am using python 3.5, firefox 45 (also tried 49) and selenium 3.0.1
I tried:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
Then I got the error message:
C:\Users\A\AppData\Local\Programs\Python\Python35\python.exe
C:/Users/A/Desktop/car/test.py
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/A/Desktop/car/test.py", line 4, in <module>
driver = webdriver.Firefox()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x0000000000EB8278>>
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
What can I do? Any help is much appreciated!
If you are using firefox ver >47.0.1 you need to have the [geckodriver][1] executable in your system path. For earlier versions you want to turn marionette off. You can to so like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['marionette'] = False
driver = webdriver.Firefox(capabilities=capabilities)

selenium webdriver not working

Selenium version: 2.53.6
Firefox version: 47.0.1
Chrome version: 51.0.2704.106 m
Now if I want to use them like that:
from selenium import webdriver
driver = webdriver.Firefox()
driver2 = webdriver.Chrome()
i get an error:
FileNotFoundError: [WinError 2]
i even checked the manual twice that its the correct way to code it.
So why cant it find the browsers even though everything is updated to latest version?
Firefox and chrome work fine if I use them as person.
edit:
can provide error code in comment so here it is, (srry some parts are in german, as it is the main language istalled on my pc):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
driver = webdriver.Firefox()
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 80, in __init__
self.binary, timeout)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 67, in launch_browser
self._start_from_profile_path(self.profile.path)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 90, in _start_from_profile_path
env=self._firefox_env)
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
and for Chrome its:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
driver2 = webdriver.Chrome()
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
the wierd thing is i did include it in path enivornment variables. I can type in 'chromedriver' in the cmd and it finds it.... so python should too.
1 more mistake:
doing last thing says some wierd stuff about: only local connetctions are allowed.
I'm not sure about running two drivers at the same time, but for the chrome part at least you may need to point it to a chrome driver (which you can download from https://sites.google.com/a/chromium.org/chromedriver/downloads
import time
from selenium import webdriver
#driver = webdriver.Chrome('..\..\..\chromedriver.exe') # Optional argument, if not specified will search path that script is running in.
driver = webdriver.Chrome('E:\AutomatedTesting\PyTestFramework\Automation\selenium\AdditionalDrivers\chromedriver.exe') #this is my path, I haven't worked out how to make the path relative to the script yet
driver.get('http://www.google.com/xhtml');
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()

Categories