Python test using selenium cannot perform simple test - python

I am trying to learn about Selenium but I am not able to get even a simple program to test. Selenium webdriver seems to not be cooperating with Firefox and I am very frustrated, so I come to Stack Overflow for help.
For background, I use Python, can install with pip, and know command line.
I am on windows 10, firefox 48, and selenium webdriver 3 with python 3.5.2
Whenever I run the selenium test, (it opens a Firefox windows and the selenium website)
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.seleniumhq.org')
I always get an error:
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: C:\ ... \AppData\Local\Temp\tmp68m5rtwt If you specified a log_file in the FirefoxBinary constructor, check it for details
It also opens a firefox window and has the link of about:blank&utm_content=firstrun(not a valid url)
I have looked across the internet for a similar situation, but nothing really close. I also tried many tutorials and made sure that I installed selenium the right way. I noticed that firefox was recently updated, but I am not sure if this has any effect.
I would appreciate any help for this, and instructions for what I should do.

Firefox 48+ doesn't support webdriver.Firefox().
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "path/to/your/firefox"
browser = webdriver.Firefox(capabilities=caps)
browser.get('http://www.seleniumhq.org')
This is what I was trying
1. download geckodriver.https://github.com/mozilla/geckodriver/releases. v.0.10.0is for selenium 3(beta).
2. add PATH your geckodriver.
4. rename it to wires
5. restart shell
6. check the version
$ wires --version
7. and run above the code.

Related

selenium.common.exceptions.SessionNotCreatedException: Message: Failed to start browser /snap/firefox/current/firefox.launcher

I am trying to open Firefox with this simple program in python, I am using the latest version of Ubuntu.
from selenium import webdriver
brow = webdriver.Firefox()
But I am getting the error message,
"selenium.common.exceptions.SessionNotCreatedException: Message: Failed to start browser /snap/firefox/current/firefox.launcher: no such file or directory"
I have tried updating firefox and using a different geckodriver.
The error is most likely a bug in GeckoDriver causing it to try to find Firefox inside Snap instead of the default location /usr/bin/firefox, because the script has been run from PyCharm that was installed through Snap.
To solve the issue, you have to unset the Snap environment variables before running the script. Actually, I found out that only two variables must be unset: SNAP_NAME and SNAP_INSTANCE_NAME:
$ unset SNAP_NAME
$ unset SNAP_INSTANCE_NAME
Surely you should take a good look at the paths you enter, however, i recommend a generic approach.
You can use a webdriver-manager that takes care of any problems in this respect automatically and in any supported operating system
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
N.B.: this is compatible with Selenium 4.x and below.

Selenium opening Brave, but not going to URL. Using Python

Yes, yes, I know everyone is going to be angry at me for asking this question again. But I have looked at every question (related to Brave, Selenium, Python and Linux) in Stack Overflow, but nothing has worked for me. Here is my problem: I run the code. No errors. Brave opens up and says it is being controlled by “automated test software” (good sign). But then it doesn't go to the URL I want it to go to. I have checked the chromedriver and Chromium version, they match. I also use Ubuntu 20.04 LTS. Last thing, I installed Brave using snap.
Here is my code:
from selenium import webdriver
brave_path = "/snap/bin/brave"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
driver = webdriver.Chrome("/bin/chromedriver_linux64/chromedriver", options=option)
driver.get("https://youtube.com/")

Selenium Opens Browser But Refuses to Execute the Next Line of Code

It looks like after opening the browser, Selenium cannot move on for some reasons I can't figure out. No error was ever displayed.
Here is my simple code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Using Chrome to access web
browser = webdriver.Chrome(executable_path = "C:\Program Files\Google\Chrome\Application\chrome.exe")
print ("done")
# Open YouTube website
browser.get("https://www.youtube.com/")
The browser opens just fine, but the print("done") statement is never executed. (In the terminal the word "done" was never printed. So, it infers that the selenium has never finished executing the command to open the browser even though the browser has opened, and I have waited for several minutes.
Thanks in advance to our wonderful StackOverflow community!
The reason is because you are using chrome.exe which is for browser. Instead you should download chromdriver.exe, Please download from here. You should download Latest stable release: ChromeDriver 94.0.4606.61 (as on 3rd-oct-2021). Keep that in your automation directory and any directory of your preference.
driver_path = r'C:\\Users\\username\\Desktop\\Automation\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = driver_path)
Note that, in place of driver_path, you should give the path where you've kept the chromdriver.exe
You should use chromedriver.exe instead of the path to your chrome.exe.
Download the chromedriver suitable for your chrome version, from here:
donwload Chromedriver.exe
Afterwards, do something like this:
browser = webdriver.Chrome("E:\YourPathToChromeDriver\chromedriver.exe")
Try these steps:
Check your Google Chrome version here "chrome://settings/help"
Download chromedriver.exe from "https://chromedriver.chromium.org/downloads"
Change executable path to newly downloaded file.
As mentioned above, this issue is the chrome.exe. You need to use a chromedriver instead. You can download one manually here https://chromedriver.chromium.org/downloads and then set the path to it like you did to the chrome.exe.
However, instead of downloading the chromedriver manually, I recommend using a library on GitHub which does that for you and loads it from cache if there is already one installed. (I'm not the owner nor the maintainer of this repository, but I do find it rather simple to use.)
https://github.com/SergeyPirogov/webdriver_manager
First you'd need to pip install webdriver-manager and then you can use it as following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

“Unable to find a matching set of capabilities” with Selenium and Python and current Firefox on Windows 10

I have the following installed:
Window 10 (version 1709)
Firefox v59 (64-bit)
Python 3.6.5
Selenium 3.11.0 for Python
GeckoDriver 0.20.0
There is a sample test called "Example 0" from the official Selenium Python site: https://pypi.python.org/pypi/selenium. This is the entire test:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
When I run it, it complains "selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities"
Searching Stack Overflow for a solution, I find other suggested solutions, like this test:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
browser = webdriver.Firefox(capabilities=cap, firefox_binary=binary)
browser.get('http://google.com/')
Running that, I get the following error:
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: C:\Users\Username\AppData\Local\Temp\tmp4t_plvms If you specified a log_file in the FirefoxBinary constructor, check it for details.
I have Firefox v59, so the error says I should use GeckoDriver. Problem is, the first test above ("Example 0") used the GeckoDriver, and that had the "Unable to find a matching set of capabilities" error. The proposed solution to that error is to set "cap["marionette"] = False" in the second test, but that approach fails.
I just can't win. How can I resolve this? I'd like to successfully run the "Example 0" test from the official Selenium Python site.
(P.S. this is not a duplicate question. All other similar questions have Firefox v54 or earlier, and are able to use the "marionette = False" approach. I'm using Firefox 59 and can't use that approach.)
Turns out, I had Firefox 64-bit, Python 3.6 32-bit, and GeckoDriver 32-bit.
I uninstalled Python 32-bit, then installed Python 64-bit.
Downloaded GeckoDriver 64-bit and corrected the 'path' in Environment Variables to point to the 64-bit driver.
Restart the computer.
I also ran 'pip uninstall selenium' then 'pip install selenium', although that probably wasn't necessary.

Geckodriver not being found

I am relatively new with this, and I am currently using python 2.7 with selenium. When running the following:
from selenium import webdriver
browser = webdriver.Firefox()
I get an error saying that 'geckodriver' executable must be in PATH. I then put it in /usr/local/bin, and it still did not work. I also tried doing browser = webdriver.Firefox(executable_path=r'your\path\geckodriver'), and it still did not work. Additionally, after using chmod +x geckodriver in the terminal, it would give me an error. What solution will fix this problem?
From your question it is not clear about the OS on which you are triggering your scripts. While you work with Selenium 3.5.x you can download the geckodriver binary and place it anywhere within your system and reference it by providing the absolute path of the geckodriver binary while you initialize the webdriver instance. On my Windows 8 Pro system this is the working code:
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://www.google.com")
print("Page Title is : %s" %driver.title)
driver.quit()
Console Output:
Page Title is : Google
Perhaps on Linux systems, we have to do:
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'/absolute_path/geckodriver')
driver.get("http://www.google.com")
print("Page Title is : %s" %driver.title)
driver.quit()

Categories

Resources