Selenium connection - python

I'm trying to get a webpage with Selenium with this code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
IEdriver= 'C:\Program Files\Internet Explorer\iexplore.exe'
browser = webdriver.Ie(IEdriver)
browser.get('www.google.com')
When IE is open, it tries to connect to :
http://--port=60803/
And I can't connect to Google. Does anyone know why ?
EDIT:
The exception is :
WebDriverException("Can not connect to the Se
selenium.common.exceptions.WebDriverException: Message:
ervice C:\Program Files\Internet Explorer\iexplore.exe

You should add scheme (application layer protocol you want to use) to URL, so replace
browser.get('www.google.com')
with
browser.get('https://www.google.com')
Also there is another problem in your code:
IEdriver= 'C:\Program Files\Internet Explorer\iexplore.exe' points on IE browser binary file while webdriver.Ie() should get path to IEDriverServer.exe as value for executable_path parameter

Related

Python selenium script can't conenct to browser

I have this script that was able to connect to a browser that is run by this command:
"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222. For some reason, it stopped working.
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
driver = webdriver.Edge(options=edge_options)
driver.get(youtube.com)
print(driver.title)
Part of the error message is this:
Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222
from chrome not reachable
Two questions about this message:
Could I just connect to the IP address of the "real" browser?
Why is there written chrome instead of chromium?
This is a full error message it makes me swallow.
https://i.stack.imgur.com/mGL4d.png

Getting error while opening a page using selenium webdriver [duplicate]

When I'm executing this code with Selenium using Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
The error occurred:
Traceback (most recent call last):
File "/Users/qa/Documents/Python/try.py", line 4, in <module>
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.44.609545 (c2f88692e98ce7233d2df7c724465ecacfe74df5),platform=Mac OS X 10.13.6 x86_64)
Can someone help me? Thanks.
I had a similar error, first getting the error message:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
(unknown error: DevToolsActivePort file doesn't exist)
This was solved by adding options.add_argument("--remote-debugging-port=9230") to the ChromeOptions(). And the program runs once and I gained the same error message as above:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: headless chrome=89.0.4389.114)
The problem here was that the chrome process does not close properly in the program so the process is still active on the debugging-port. To solve this problem close the active port sudo kill -9 $(sudo lsof -t -i:9230) and simply add the following lines to the end of the code:
driver.stop_client()
driver.close()
driver.quit()
Since I didn't find this answer anywhere, I hope it helps someone.
If you have options.add_argument("--remote-debugging-port=9222") change this to options.add_argument("--remote-debugging-port=9230")
or just simply Adding options.add_argument("--remote-debugging-port=9230") fixed in my case.
This error message...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
You need to consider a fact:
As you are using Mac OS X the Key executable_path must be supported with a Value as :
'/Users/qa/Documents/Python/chromedriver'
So line will be:
driver = webdriver.Chrome(executable_path='/Users/qa/Documents/Python/chromedriver')
Note: The path itself is a raw path so you don't need to add the switch r and drop it.
Additionally, ensure that /etc/hosts on your system contains the following entry :
127.0.0.1 localhost.localdomain localhost
#or
127.0.0.1 localhost loopback
I ran into this same issue on a Windows 10 machine. What I had to do to resolve the issue was to open the Task Manager and exit all Python.exe processes, along with all Chrome.exe processes. After doing this,
I am facing the same error and add the close code solve the problem, the code finnaly block look like this:
#staticmethod
def fetch_music_download_url(music_name: str):
chrome_driver_service = Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install())
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--remote-debugging-port=9230")
driver = webdriver.Chrome(service=chrome_driver_service, options=chrome_options)
try:
driver.maximize_window()
driver.get('http://example.cn/music/?page=audioPage&type=migu&name=' + music_name)
driver.implicitly_wait(5)
// do some logic
except Exception as e:
logger.error(e)
finally:
// add the close logic
driver.stop_client()
driver.close()
driver.quit()
chrome_driver_service.stop()
the key is you should close the chrome service after using it by add chrome_driver_service.stop().hope this code could help other people facing the same issue.
This worked for me on WINDOWS OS
from selenium import webdriver
import time
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)
driver.get(url)
time.sleep(3)
page = driver.page_source
driver.quit()
soup = BeautifulSoup(page, 'html.parser')
Hope you'd find this useful. I also used it more comprehensively here.

ChromeDriver crashes with dialog-box prompt - Chromedriver stopped working

After running pip selenium and downloading chromedriver.exe to C:/chromedriver/chromedriver.exe directory. Running my program results in Chrome not being able to open the URL and the following message prompts in a dialog-box:
Chromedriver stopped working
This is my attempt at testing that the source-page could be accessed.
import requests
from selenium import webdriver
Base_url = "https:/www.facebook.com"
driver = webdriver.Chrome(r'C:/chromedriver/chromedriver.exe')
driver.get(Base_url)
print (driver.page_source)
Can someone help me sort this out?
Thank you.
To open Chrome Browser using chromedriver binary you need to pass the argument executable_path along with the absolute path of the chromedriver binary and invoke the proper url as follows :
from selenium import webdriver
Base_url = "https://www.facebook.com/"
driver = webdriver.Chrome(executable_path=r'C:/chromedriver/chromedriver.exe')
driver.get(Base_url)
print (driver.page_source)

When trying to use a custom profile in Chrome with selenium webdriver in Python I keep getting this error

WebDriverException: Message: Can not connect to the Service /usr/lib/chromium/chromium
One of the only places which seemed to show you how, but that might only work on Windows. That's where I got the code from. How to load default profile in chrome using Python Selenium Webdriver?
My code.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/home/me/.config/chromium/Default") #Path to your chrome profile
w = webdriver.Chrome(executable_path="/usr/lib/chromium/chromium", chrome_options=options)
w.get("https://google.com")
The browser opens up, pauses, doesn't go to the URL, then gives me that error message.
This code stops giving me the error message, but my user data does not show up.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
config = "_config/"
chromedriver = config+"chromedriver"
options.add_argument("--profile-directory='Default'") #Path to your chrome profile
w = webdriver.Chrome(chromedriver, chrome_options=options)
w.get("https://google.com")
And if I used this executable path instead, with everything else being the same, it opens up the browser with all of the desired user data, but then gives this error. WebDriverException: Message: Service chromium unexpectedly exited. Status code was: 0 The browser stays open:
w = webdriver.Chrome(executable_path="chromium", chrome_options=options)

Error adding adblock to Chrome python selenium webdrive

I tried to add adblock to the selenium using this code:
chop =webdriver.ChromeOptions()
chop.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options=chop)
I got this error:
'OSError: Path to the extension doesn't exist'
what should I do?
You need to provide the full path:
from os import path
chop =webdriver.ChromeOptions()
chop.add_extension(path.abspath('Adblock-Plus_v1.4.1.crx'))
driver = webdriver.Chrome(chrome_options=chop)

Categories

Resources