Python selenium gives data; in Chrome browser - python

Trying to create a webscraper via Selenium. Below code only gives data; in the browser and ends up with error.
from selenium import webdriver
options = webdriver.ChromeOptions()
website = ""
path = "/Users/lilly/Downloads/chromedriver"
driver = webdriver.Chrome(path)
driver.get(website)
#all_matches_button = driver.find_element_by_xpath('')
#all_matches_button.click()
#driver.quit()
Chrome browser: Version 104.0.5112.81 (latest)
Chrome driver: 104.0.5112.79 (latest)
Error message in Python:
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
Tried to reinstall Chrome driver, but not luck. (https://sites.google.com/chromium.org/driver/)
Currently working on my Windows, but above code works on my macbook. I noticed on my Macbook that the version of the Chrome browser and driver are the same (104.0.5112.79).
Also have to close all browser tabs before running the code. otherwise it will result in the below error:
InvalidArgumentException: Message: invalid argument: user data directory is already in use error using --user-data-dir
Also quite annoying..
Thanks.
L.

For Windows, you have to provide the file extension. In your Mac, the chrome driver executable file name is chromedriver. But for Windows, it is chromedriver.exe.
Change your path to the following and it will work. Make sure the executable file path is provided not just the folder.
path = "/Users/lilly/Downloads/chromedriver.exe"

Related

Expected binary browser location - traceback in Python

I've just started building my first bot and I'm struggling with the first step: automating a browser.
Here's my code:
from selenium import webdriver
browser = webdriver.Firefox(executable_path="/Users/ker/Downloads/geckodriver")
browser.get("https://app.finxter.com/")
When I try running the code I get the following error:
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
I've installed the geckodriver unix executable and specified the path, but for some reason it still won't work and I can't really understand the error message.
You may get this error message for two reasons:
Firefox isn't installed in your system.
Firefox isn't installed in the default location within your system.
Solution:
If Firefox isn't installed, install it on your system.
If firefox isn't installed at the default location, you need to pass the path of the firefox executable through an Option() instance:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r"C:/location/firefox.exe"
driver = webdriver.Firefox(options=options, executable_path="/Users/ker/Downloads/geckodriver.exe")
driver.get('https://app.finxter.com/')
Please check if your driver is executable
you can change the permission with the command below.
chmod +x /Users/ker/Downloads/geckodriver

Selenium - Chromedriver filenotfound error message

I am trying to use selenium to test a webpage I am building.
I downloaded chrome driver and placed the executable in the path below.
I know the path is correct.
driver = webdriver.Chrome(executable_path=r'C:\Python\chromedriver.exe')
What can I do to address this or what am I missing?
Try calling the file without the ".exe" extension.
I use chromedriver but I never need to call the extension.
driver = webdriver.Chrome('C:\Python\chromedriver')

python-selenium.common.exceptions.WebDriverException: Message: Unknown error

I'm up with python 3.8 and selenium but recently I downloaded the latest edge web driver zip file and runned the mswdedriver.exe from that and typed this code in my ide:
from selenium import webdriver
browser = webdriver.Edge('‪F:\za\python\Assistant\msedgedriver.exe')
browser.maximize_window()
browser.get(url='http://seleniumhq.org/')
but I see this error:
selenium.common.exceptions.WebDriverException: Message: 'MicrosoftEdgeDriver' executable needs to be
in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687
Can you help me friends?
Thanks in advance.
You need to give a path to the webdriver executable when you load a webdriver, or have it stored as an environment variable:
webdriver.Edge(executable_path="path/to/executable")
A web driver is essentially a special browser application, you must install that application before you can run anything with it.
Here's Edge's web driver download page. Or you can use the link from the error message http://go.microsoft.com/fwlink/?LinkId=619687
Here's a similar question Python Selenium Chrome Webdriver
The backslashes in the executable path need to be escaped, per Python syntax:
browser = webdriver.Edge('‪F:\\za\\python\\Assistant\\msedgedriver.exe')
This problem appears to me
you must put in "Bin Folder" the file "MicrosoftWebDriver.exe" as is , edit the previous name of edge webdriver to be "MicrosoftWebDriver.exe" and put it in the "Bin Folder"
You need to download the browser driver from here
After the download completes, extract the driver executable to your preferred location. Add the folder where the executable is located to your PATH environment variable.

why wont selenium module work properly for me

I installed the selenium module with pip installer. Then I tried to make the code to open firefox, then open a new tab to go to google.
Code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
The expected result should have opened firefox and then go to google.com.
But what actually happened was the program didn't produce any error, it just didn't open firefox, and the screen wasn't frozen either.
Download a matching version of Geckodriver and unpack the geckodriver.exe to the location where current user can execute programs from (normally it's any place inside your home folder)
Amend your code to include the location of the aforementioned geckodriver like:
driver = webdriver.Firefox(executable_path="/path/to/the/geckodriver/binary")
If this doesn't help - provide the path to the Firefox executable as well:
driver = webdriver.Firefox(executable_path="/path/to/the/geckodriver/binary", firefox_binary="/path/to/firefox/binary")
Instead of steps 2 and 3 you can add both firefox and geckodriver to your OS PATH
References:
Selenium with Python - Getting Started
Selenium With Python
Selenium using Python - Geckodriver executable needs to be in PATH

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python

I am trying to open Firefox with selenium,i tried
from selenium import webdriver
driver=webdriver.Firefox()
But i got the following error:
selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.
Selenium using Python - Geckodriver executable needs to be in PATH
I tried
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/usr/bin/firefox')
browser = webdriver.Firefox(firefox_binary=binary)
Also tried
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps['marionette'] = True
caps['binary'] = '/usr/bin/firefox'
d = webdriver.Firefox(capabilities=caps)
`but still did not work.
However, when i tried using the above code replacing the last line with
d=webdriver.Firefox(capabilities=caps,executable_path='/usr/bin/firefox') and having my Firefox closed from background it would open Firefox but I can't simply d.get("https://www.google.com") it gets stuck on Linux homepage and doesn't open anything.
After typing whereis firefox in terminal i got /usr/bin/firefox,also if it matters i use python 2.7
Note: I hope this isn't a duplicate of the above link because i tried the answers and it didn't fix it.
I installed geckodriver from github, and tried browser=webdriver.Firefox(executable_path="geckodriver") ,I have placed the driver is the same directory.
It is still not clear why you are seeing the error as:
selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.
In majority of the cases the common PATH related error is associated with geckodriver.
However, while working with Selenium 3.x you need to download the latest GeckoDriver from mozilla/geckodriver and save it anywhere in your system and provide the absolute path of the GeckoDriver through the argument executable_path.
The following code block works perfecto to open Firefox Nightly Browser (installed at customized location):
Code Block:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = '/path/to/firefox'
driver = webdriver.Firefox(firefox_options=options, executable_path='/path/to/geckodriver')
driver.get('http://google.com/')
print("Page title is: %s" %(driver.title))
driver.quit()
Console Output:
Page title is: Google

Categories