I'm trying to get all results from a predefined google search, but I'm stuck at the following error:
Traceback (most recent call last):
File "C:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\python36\lib\subprocess.py", line 992, 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\amzar\Desktop\pyscripts\selen.py", line 23, in <module>
get_results("dog")
File "C:\Users\amzar\Desktop\pyscripts\selen.py", line 6, in get_results
browser = webdriver.Firefox()
File "C:\python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I checked for 30 minutes all possibilities with importing, but I can't figured it out.
from selenium import webdriver
def get_results(search_term):
url = "http://startpage.com"
browser = webdriver.Firefox()
browser.get(url)
search_box = browser.find_element_by_id("query")
search_box.send_keys(search_term)
search_box.submit()
try:
links = browser.find_elements_by_xpath("/ol[#class='web_regular_results']//h3//a")
except:
links = browser.find_elements_by_xpath("//h3//a")
results = []
for link in links:
href = link.get_attribute("href")
printf(href)
results.append(href)
browser.close()
return results
get_results("dog")
Related
Here is the python code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import subprocess
EXTENSION_PATH = "C:/Users/HP/AppData/Local/Google/Chrome/User Data/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn/10.17.0_0.crx"
opt = webdriver.ChromeOptions()
opt.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome(chrome_options=opt)
time.sleep(1)
I try to run the program I get the following error:(file name: 1.py)
"C:/Program Files/Python310/python.exe" c:/Users/HP/Desktop/py/1.py
c:\Users\HP\Desktop\py\1.py:27: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(chrome_options=opt)
Traceback (most recent call last):
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Program Files\Python310\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\Python310\lib\subprocess.py", line 1438, 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\HP\Desktop\py\1.py", line 27, in <module>
driver = webdriver.Chrome(chrome_options=opt)
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__
self.service.start()
File "C:\Users\HP\AppData\Roaming\Python\Python310\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
I put all the required files in the patch.
I am using a windows
Change:
driver = webdriver.Chrome(chrome_options=opt)
with:
driver = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe", options=opt)
You should be good to go.
Your effective code block will be:
EXTENSION_PATH = "C:/Users/HP/AppData/Local/Google/Chrome/User Data/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn/10.17.0_0.crx"
opt = webdriver.ChromeOptions()
opt.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe", options=opt)
I'm trying use pywinauto to start browser, Chrome or FireFox. But I can`t find the name of application:
from pywinauto.application import Application
if self.browser == "chrome":
app = Application(backend="uia").start ("C:\Program Files\Google\Chrome\Application\chrome.exe --force-renderer-accessibility")
time.sleep(10)
app["Кто использует Chrome?"].print_control_identifiers()
I use inspcet.exe to get elements name:
inspect.exe data:
Name: "Кто использует Chrome?" (cyrylic/russian name)
ControlType: UIA_PaneControlTypeId (0xC371)
LocalizedControlType: "панель"
BoundingRectangle: {l:388 t:141 r:1428 b:938}
IsEnabled: true
ProcessId: 61944
RuntimeId: [2A.3206BA]
FrameworkId: "Win32"
ClassName: "Chrome_WidgetWin_1"
...
The window is open normally - it's the Chrome pane for chooosing user-profile.
But error:
Traceback (most recent call last):
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\application.py", line 258, in __resolve_control
criteria)
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
raise err
pywinauto.timings.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/shulya403/Shulya403_works/all_gid_2/Scraper/ya_scrape_experiment1.py", line 346, in <module>
CromeWindwow = WinChrome(browser="chrome")
File "C:/Users/shulya403/Shulya403_works/all_gid_2/Scraper/ya_scrape_experiment1.py", line 333, in __init__
app[r"Кто использует Chrome?"].print_control_identifiers()
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\application.py", line 613, in print_control_identifiers
this_ctrl = self.__resolve_control(self.criteria)[-1]
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
raise e.original_exception
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
func_val = func(*args, **kwargs)
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
File "C:\Users\shulya403\Shulya403_works\all_gid_2\venv\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'best_match': 'Кто использует Chrome?', 'backend': 'uia', 'process': 65412}
One time it has no timeout, and it show English alternative of names [u'Кто использует Chrome?', 'Pane', '...']
variant:
app.Pane.print_control_identifiers()
worked one time only. But next run returned to the error.
I try with FireFox window too, with same result.
from selenium import webdriver
chromedriver_location = "/Users/Sony/Desktop/XUAN/Python/chromedriver_win32/chromedriver"
driver = webdriver.Chrome()
driver.get('https://sapsnkra.moe.gov.my/ibubapa2/')
input(" ")
PS C:\Users\Sony\Desktop\XUAN\Python> python -u "c:\Users\Sony\Desktop\XUAN\Python\Python Project\tempCodeRunnerFile.py"
Traceback (most recent call last):
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 951, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1420, 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\Sony\Desktop\XUAN\Python\Python Project\tempCodeRunnerFile.py", line 3, in
driver = webdriver.Chrome()
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in init
self.service.start()
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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
You can actually use webdriver-manager to automatically manage your chromedriver version.
You can use it like this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://sapsnkra.moe.gov.my/ibubapa2/')
input(" ")
I cant run or install selenium on Ubuntu 18.04.3 ...
I followed this tutorial: https://medium.com/#hoppy/how-to-test-or-scrape-javascript-rendered-websites-with-python-selenium-a-beginner-step-by-c137892216aa also with the digitalocean setup.
I tried different things...
I installed the geckodriver in /var/bin
in /var/local/bin
added both to PATH
tried with a path in the code and without
tried with firefox (preferred) ...
nothing helps
This Code:
import time
from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
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()
Ends up with:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
selenium.common.exceptions.WebDriverException:: command not found
(venv) root#EtherScrape:/var/test# python /var/test/server_test.py Traceback (most recent call last):
File "/var/test/server_test.py", line 5, in <module>
driver = webdriver.Chrome('/usr/bin/chromedriver') # Optional argument, if not specified will search path.
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=83.0.4103.61 (94f915a8d7c408b09cc7352161ad592299f384d2-refs/branch-heads/4103#{#561}),platform=Linux 4.15.0-66-generic x86_64)
This Code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(firefox_options=options)
With that error:
Traceback (most recent call last):
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/test/server_test.py", line 6, in <module>
driver = webdriver.Firefox(firefox_options=options)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/var/venv/lib/python3.6/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 0x7ff3109ced68>>
Traceback (most recent call last):
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
self.stop()
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Please help.
Tried many solutions so far but no one worked out...
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
Works for me at the chrome driver.
When i'm running this code:
from selenium import webdriver
import time
driver = webdriver.Chrome('C:/Program Files
(x86)/Google/Chrome/Application/chrome.exe')
driver.get('https://www.google.com/')
It only opens my browser but not the URL that was passed as a parameter to driver.get().
Given below is the traceback:
Traceback (most recent call last):
File "C:\Users\efiqq\OneDrive\Plocha\Python\Link.py", line 4, in <module>
driver = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chrome.exe')
File "C:\Users\efiqq\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
self.service.start()
File "C:\Users\efiqq\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
self.assert_process_still_running()
File "C:\Users\efiqq\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service C:/Program Files (x86)/Google/Chrome/Application/chrome.exe unexpectedly exited. Status code was: 0
The path you have to pass is not to chrome.exe. You have to point to the chromedriver.exe;
You can get it here: http://chromedriver.chromium.org/downloads