Firstly, I'd just like to say my level of python programming is absolute beginner so please be patient!
I've installed Python 3.7.4, along with some packages such as numpy, xlwings and (most importantly) selenium.
I've downloaded the this onto my 64-bit windows 10 laptop and followed the advice of the Python forum to address issues particular to Windows but still, when I try to run a slightly modified version of this example code which basically opens a browser (the sample opens firefox, I'm using Chrome - they both have this same issue!), navigates to facebook and logs-in.
My code is:
from selenium import webdriver
import time
username = 'fb_email#email.com'
password = 'fb_password'
url = 'https://www.facebook.com/'
driver = webdriver.Chrome(executable_path=r"C:\\Python37\\Lib\\site-packages\\selenium\\webdriver\\chrome\\webdriver.py")
driver.get(url)
driver.find_element_by_id('email').send_keys(username)
driver.find_element_by_id('pass').send_keys(password)
time.sleep(2)
driver.find_element_by_id('loginbutton').click()
When I run this code I'm getting the following error message:
Traceback (most recent call last):
File
"C:/Python37/Codes/Test.py", line 9, in
driver = webdriver.Chrome(r"C:\Python37\Lib\site-packages\selenium\webdriver\chrome\webdriver.py")
File
"C:\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 73, in init
self.service.start() File "C:\Python37\lib\site-packages\selenium\webdriver\common\service.py",
line 76, in start
stdin=PIPE) File "C:\Python37\lib\subprocess.py", line 775, in init
restore_signals, start_new_session) File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo) OSError: [WinError 193] %1 is not a valid Win32 application
You'll notice one other change I've made from the original code is I've include the full file path (explicitly referencing Webdrive.py file) to deal with the "permissions" error.
Basically, I just want to get to the stage I can run this code and I can do I know I can run selenium on python in principal and deal with the nested looping within the python code at a later date so any help/suggestions would be greatly appreciated.
Root Cause:
You are not providing the correct path to the executable_path in line # 9. executable_path should point to the chromedriver.exe path not the p.py path.
How to fix:
Update the line#9 with the correct path to the chromedriver.exe.
# make sure you add `.exe` file name too at the end like `chromedriver.exe`.
driver = webdriver.Chrome(executable_path=r"pathToChromedriver")
Related
I am using selenium with python and have downloaded the chromedriver for my windows computer from this site. After downloading the zip file, I unpacked the zip file to my downloads folder. Then I add the path to the the Environment Variable "Path".
I want to get information from the site, but when I run code, I get this error and comletely don't understand what is it about.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
url = 'any_url'
browser.get(url)
if browser.find_element_by_id("yearlist_1").get_attribute("type") == "checkbox":
print("Element is a checkbox")
else:
print("Element is not a checkbox")
The error:
*'reg' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
File "C:/Users/miair/Python/script_gks/code_for_ticks.py", line 4, in <module>
browser = webdriver.Chrome(ChromeDriverManager().install())
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\chrome.py", line 28, in install
driver_path = self.download_driver(self.driver)
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\manager.py", line 36, in download_driver
driver_version, is_latest = self.__get_version_to_download(driver)
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\manager.py", line 27, in __get_version_to_download
return self.__get_latest_driver_version(driver), True
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\manager.py", line 21, in __get_latest_driver_version
return driver.get_latest_release_version()
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\driver.py", line 58, in get_latest_release_version
self._latest_release_url + '_' + chrome_version(self.chrome_type))
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\utils.py", line 114, in chrome_version
.format(cmd)
ValueError: Could not get version for Chrome with this command: reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version*
Sorry, I am very new to Python, but if somebody could help, I'll be very pleased and thankful.
I solved problem. I don't understand why I've hadn't think of this solution before - all you need to do is to write the path of your chromedriver (only if you have a correct version)
browser = webdriver.Chrome('path')
important to know for people searching for the same error and ending here. chrome needs to have started at least once to generate the reg key in windows.
I suspect that you are using a selenium web-driver that is not matching your chrome driver version.
you can update your chrome using the following link:
chrome://settings/help
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 4 years ago.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
Game_Pin = input('Enter your PIN: ')
NickNAME = input('Enter your nickname: ')
def Enter_Press():
browser.find_element_by_name("Enter").click()
def Kahoot_Spammer(Game_Pin, NickNAME):
chromedriver = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
browser = webdriver.Chrome(chromedriver)
browser.get('https://kahoot.it/')
game_pin = browser.find_element_by_id("Game PIN")
Name = browser.find_element_by_id("Nickname")
game_pin.send_keys(Game_Pin)
Enter_Press()
Name.send_keys(NickNAME)
Enter_Press()
Kahoot_Spammer(Game_Pin, NickNAME)
Wondering what I have done wrong in the code above. The file I have put into the path and have ran the
icacls "pathtochromedriver" /grant Users:F
command through the command prompt but it still didn't fix the problem. I have been stuck on this problem for quite some while now.
Traceback (most recent call last):
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, 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\ovvip\AppData\Local\Programs\Python\Python36-32\KahootSpammer.py", line 28, in <module>
Kahoot_Spammer(Game_Pin, NickNAME)
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\KahootSpammer.py", line 13, in Kahoot_Spammer
chromedriver = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
self.service.start()
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\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: 'chromedriver_win32' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Any ideas on how to fix this? Every time I ask this question I usually end up with a answer that will fix one thing but then another problem arises.
Try using webdriver.Chrome(executable_path=r"C:\WebDriver\bin\chromedriver_win32")
If that doesn't solves the problem than I think there might be an issue with the version of chrome driver you are using download chromedriver from this path
Chromedriver Download Link
and paste it in the directory
and test it for this code it should run smooth without any error
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path=r"C:\WebDriver\bin\chromedriver.exe")
for j in range(2,8):
for i in range(2,8):
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
str1 = '//*[#id="offers_table"]/tbody/tr['
str2 = ']/td/table/tbody/tr[1]/td[2]/h3/a/span'
str3 = str1 + str(i) + str2
a = driver.find_element_by_xpath(str3).text
print a
Comment below if you face any issue
for one thing:
chromedriver = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
browser = webdriver.Chrome(chromedriver)
^^ Here you create a webdriver.Chrome instance named chromedriver. Then on the next line, you attempt to create another webdriver.Chrome instance... but this time passing the existing instance to it? Not sure why you are attempting to create multiple instances of it at all, but that code is definitely not going to work. Just create a single instance of webdriver.Chrome and use it:
browser = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
I want to use selenium to scrape off some website. I can't access the website via my own internet connection, so I need to use browsec mozilla addon for that.
I am unable to launch firefox with selenium with the add-on enabled.
Here is what I have tried:
import selenium
from selenium import webdriver
url = "http://url"
profile = webdriver.FirefoxProfile()
profile.add_extension('browsec#browsec.com.xpi')
#profile.add_extension("C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec#browsec.com.xpi")
driver = webdriver.Firefox(firefox_profile=profile)
if __name__ == "__main__":
driver.get(url)
driver.wait(5)
driver.quit()
I have tried putting the extension in the same directory where my script is and using the following
profile.add_extension('browsec#browsec.com.xpi')
which gives me this error when I run:
Traceback (most recent call last): File
"C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 346, in _addon_details
with open(os.path.join(addon_path, 'install.rdf'), 'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
'C:\Users\Usr\AppD
ata\Local\Temp\tmp0hny31u3.browsec#browsec.com.xpi\install.rdf'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "test.py", line 7, in
profile.add_extension("browsec#browsec.com.xpi") File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 95, in add_extension
self._install_extension(extension) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 274, in _install_extension
addon_details = self._addon_details(addon) File "C:\Python36\lib\site-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:
'C:\\Users\\Usr\\AppData\\Local\\Temp\\tmp0hn
y31u3.browsec#browsec.com.xpi\\install.rdf'", )
I also tried giving the path to the extension:
profile.add_extension("C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec#browsec.com.xpi")
And I ran into this error:
profile.add_extension("C:\Users\Hassan\AppData\Roaming\Mozilla\Firefox\Profi
les\n5jwlj9l.default\extensions\browsec#browsec.com.xpi")
^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio n 2-3: truncated
\UXXXXXXXX escape
Formatting the path string like below doesn't help either.
profile.add_extension(r"C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec#browsec.com.xpi")
I get the following:
Traceback (most recent call last): File "test.py", line 7, in
profile.add_extension(r"C:\Users\Hassan\AppData\Roaming\Mozilla\Firefox\Prof
iles\n5jwlj9l.default\extensions\browsec#browsec.com.xpi") File
"C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 95, in add_extension
self._install_extension(extension) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 274, in _install_extension
addon_details = self._addon_details(addon) File "C:\Python36\lib\site-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:
'C:\\Users\\usr\\AppData\\Local\\Temp\\tmp1he
0fym_.browsec#browsec.com.xpi\\install.rdf'", )
How do I configure selenium to run firefox with browsec enabled by default?
I found this article rather helpful.
Instead of adding the extension to the profile, you install it after the browser has been created:
from selenium import webdriver
driver = webdriver.Firefox()
# This installs adblock plus
driver.install_addon("/home/your_username/coding/Project/seleniumTest/adblock.xpi", temporary=True)
driver.get('https://www.stackoverflow.com')
Be sure to add the .xpi to your project folder!
You can try to create profile on firefox browser like - On windows Run --> type
"firefox.exe -P"
It will open profile manager. Create new profile. Start firefox from that profile, add plugins. And use that same profile with code..Sometime it worked for me..
Sorry for my English))
Most likely you are using the new version of Firefox (Quantum - from the 57th version inclusive). In newer versions of Firefox, the extension metadata is not stored in the install.rdf file, but in the manifest.json file. Selenium does not know this yet (in version 3.11, and learns only in 3.14). Therefore, when trying to connect an extension, it looks for habit install.rdf.
Here the author wrote a class that slightly changes the connection function of the extension, and instead of install.rdf, selenium looks for metadata in manifest.json.
What you need to do:
# Add Import
import json
import os
import sys
from selenium.webdriver.firefox.firefox_profile import AddonFormatError
# Add class
class FirefoxProfileWithWebExtensionSupport(webdriver.FirefoxProfile):
def _addon_details(self, addon_path):
try:
return super()._addon_details(addon_path)
except AddonFormatError:
try:
with open(os.path.join(addon_path, 'manifest.json'), 'r') as f:
manifest = json.load(f)
return {
'id': manifest['applications']['gecko']['id'],
'version': manifest['version'],
'name': manifest['name'],
'unpack': False,
}
except (IOError, KeyError) as e:
raise AddonFormatError(str(e), sys.exc_info()[2])
# Declare Firefox_profile written class
profile = FirefoxProfileWithWebExtensionSupport()
Further as usual)))
Good luck)))
I am stuck with my script at a point. The script is this
import subprocess
import os
def Windows():
SW_MINIMIZE = 6
info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = SW_MINIMIZE
print(os.path.isdir("C:\Program Files (x86)"))
while True:
try:
subprocess.Popen(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe', startupinfo=info)
except WindowsError:
subprocess.Popen(r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', startupinfo=info)
else:
try:
subprocess.Popen(r'C:\Program Files\Mozilla Firefox\firefox.exe', startupinfo=info)
except WindowsError:
subprocess.Popen(r'C:\Program Files\Google\Chrome\Application\chrome.exe', startupinfo=info)
What I want to do is check if the computer is 64 bit or 32 bit (as I want to open the browser without a window using subprocess.) to locate the browsers chrome or firefox, depending on which one the user has ( I am assuming that they have either one of them). Since the path for chrome and firefox varies in 64 vs 32 bit computers (Program Files and Program Files (x84)), I came up with this script which detects if x86 folder exists or not. If it does, it continues on the folder for searching for the browsers. However, if it doesn't, it assumes it is 32-bit and searches for Program Files folder and in that folder it searches for the browsers.
However, when I run the script I get this error
Traceback (most recent call last):
File "C:\Users\Charchit\Desktop\via.py", line 29, in <module>
Windows()
File "C:\Users\Charchit\Desktop\via.py", line 13, in Windows
subprocess.Popen(r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', startupinfo=info)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
However, in my script it should not even go to while True Section because I have a 32 bit system and x86 folder doesn't exist!
You're not actually checking if os.path.isdir("C:\Program Files (x86)"). You're just printing it.
Instead of
print(os.path.isdir("C:\Program Files (x86)"))
while True:
You need to do
if os.path.isdir(r"C:\Program Files (x86)"):
Side note:
Both chrome and firefox traditionally place themselves on the path, so there's a good chance you can just do subprocess.Popen('firefox.exe') / subprocess.Popen('chrome.exe').
for creating the path use the built-ins python functions that won't mess up the path
if os.path.exists(os.path.join('C:', os.path.sep(), 'Program Files')):
# do your stuff
I have encountered an error which I am not able to resolve.
I am trying to perform the easiest set of commands that will perform a tBLASTn algorithm,
looking for a sequence (sequence specified as a "pytanie.fasta" file) in a database (also specified as file -> cucumber.fasta). The result will be saved in the "wynik.txt" file.
The code looks as following:
from Bio.Blast. Applications import NcbitblastnCommandline
database = r"\Biopython\cucumber.fasta"
qr = r"\Biopython\pytanie.fasta"
output = r"\Biopython\wynik.txt"
e = raw_input("Enter e-value: ")
tblastn_cline = NcbitblastnCommandline(cmd='blastn', db=database, query=qr, out=output, evalue=e, outfmt=7)
print tblastn_cline
stdout, stderr = tblastn_cline()
And the error I get:
File "C:\Users\IBM_ADMIN\Desktop\PYTHON\Workspace\Biopython\blast.py", line 20, in <module>
stdout, stderr = tblastn_cline()
File "C:\Python27\lib\site-packages\Bio\Application\__init__.py", line 435, in __call__
shell=(sys.platform!="win32"))
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I am using:
Eclipse SDK Version: 3.7.1
Python version 2.7
OS: 64 bit Windows 7
I have tried this also on 32-bit Windows XP and it produces the same error.
Biopython package should work fine since it went through the tests suggested by biopython website. I have also tried other formats of the path where the files are located, but it did not work. My friend uses the same code on Ubuntu and it works fine.
Does anybody know how to fix this error?
What are the paths of the files?
The path r"\Biopython\cucumber.fasta", for example, is an absolute path on the current drive (because it starts with a backslash and no drive letter), which I think in your case is r"C:\Biopython\cucumber.fasta". Is that correct?