Selenium test with Python in Internet Explorer - python

I have written a script that opens a web browser using python and Selenium. It works fine with Firefox using the following code:
from selenium import webdriver
driver = webdriver.Firefox()
When I replace Firefox with IE (the suggested value when I start typing), I get the message IEDriver executable needs to be available in the path.
from selenium import webdriver
driver = webdriver.IE()

Download IE Drivers based on your OS (Windows 32 or 64 bit)
a. Download Windows 32 bits driver
OR
b. Download Windows 64 bits driver
Extract the zip and copy IEDriverServer.exe file to some location e.g. E:\IEDriver
Write the following script
from selenium import webdriver
browser = webdriver.Ie("e:\\IEDriver\\IEDriverServer.exe")
Run the script, it should open IE browser...

Selenium with Python bindings in IE:
There are 2 ways to run Selenium python tests in Internet Explorer. I'm considering Windows (Windows 10 in my case):
Prerequisite: Download IE Driver based on your OS from the site: http://docs.seleniumhq.org/download/
32 bit Windows IE
64 bit Windows IE
Way 1:
i) Extract the downloaded zip file in a directory/location of your choice
ii) Set the executable path in your code as below:
self.driver = webdriver.Ie(executable_path='D:\Selenium_RiponAlWasim\Drivers\IEDriverServer_x64_2.42.0\IEDriverServer.exe')
OR,
self.driver = webdriver.Ie("D:\\Selenium_RiponAlWasim\\Drivers\IEDriverServer_x64_2.42.0\\IEDriverServer.exe")
Way 2:
i) Simply paste the IEDriverServer.exe under /Python/Scripts/ (In my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:
self.driver = webdriver.Ie()

It means exactly that. Selenium needs the executable to work with IE.
A quick google search led me to this. You need to download the executable and place it somewhere visible. Also, taking a look at this should help clear some things about PATH variables.

In selenium 4 you would need to use service method otherwise you would get depreciated error: You no longer need to download IE driver.
Here are compatible codes for Selenium 4.x IE:
# Internet Explorer Browser version
from selenium import webdriver
from selenium.webdriver.ie.service import Service
from webdriver_manager.microsoft import IEDriverManager
driver = webdriver.Ie(service=Service(executable_path=IEDriverManager().install()))
driver.get('https://www.google.com')

Related

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())

Running a selenium based exe to scrape data

What is working
I have created a Python file that opens a Firefox browser using Selenium and scrapes data to a excel file. I have also converted this file to exe. No errors were created during exe. This was done by Pyinstaller.
What is not working.
When I run the new created exe on official laptop it doesn't open a browser or give any output. Just a command window opens and closes immediately.
What is my intention.
My intention is to share this script with colleagues so that the data needed can be easily scraped to excel file on any PC and would be efficient process rather than looking up the same on website. I would not want to them to have any admin access to change anything on their PC. Also, I do not want them to have multiple browsers opening up on their PC.
Question
Do I need to install and provide geckodriver path on every PC? Perhaps this is the reason Firefox is not opening despite being available?
Code Below
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
productName = []
number = []
divList = []
driver = webdriver.Firefox()
driver.get("website")
radioBtn = driver.find_element_by_id("ContentPlaceHolder1_company")
radioBtn.click()
element = driver.find_element_by_id("ContentPlaceHolder1_TextBoxInput1")
element.send_keys("CompanyName")
Search = driver.find_element_by_id("ContentPlaceHolder1_view1Continue")
Search.click()
driver.implicitly_wait(10)
driver.find_element_by_id("ctl00_ContentPlaceHolder1_92564").click()
driver.implicitly_wait(2)
divList = driver.find_elements_by_class_name('nopgbrk')
for div in divList:
number.append(div.find_element_by_xpath('a').text)
productName.append(div.find_element_by_xpath('span').text)
df = pd.DataFrame({'Product':productName, 'Registration': number})
df.to_excel("C:\Python\Scrapeddata\Scrape.xls")
I anticipate, the reason for this would be the incompatibility of chromedriver version and the chrome version of your official laptop.
In the development phase, the system on which it was being developed had chrome, whose version was compatible with selenium chromedriver version. So it ran and processed output successfully. But your official laptop did not have the same version that of your system's chrome, so it is not compatible.
Please check chrome version by going to settings -> About Chrome.
Download chromedriver for the same version which you checked above from here and then create exe if it supports:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Or if you know the version of your chromedriver the easiest would be to uninstall chrome on your official laptop and install chrome of the same version that of chromedriver.

How can I choose which firefox executable to use among multiple firefox binaries through Python selenium

I'm using python (v 3.65) selenium (v3.11.0) on Mac OS X (v 10.11.6) with firefox (v 59.0.3) and geckodriver (v 0.20.1). I have my usual firefox in the Applications folder and a second firefox in another folder. How can I tell python selenium to use the second firefox instead of going to the one in Applications?
I'd prefer an answer that generalises to other browsers besides firefox/geckodriver, if possible.
To choose and use one of the Firefox executable among multiple you can use the argument binary_location from firefox.options. As an example in the following code block I have used the Firefox Nightly binary to open the Firefox Nightly browser :
Code Block :
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('http://google.com/')
print("Firefox Browser Invoked")
driver.quit()
Console Output :
Firefox Browser Invoked

Python test using selenium cannot perform simple test

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.

Path to chromedriver on Linux

I put the chromedriver in a random folder. And wish to call it to use webdriver from selenium
But actually on: https://code.google.com/p/selenium/wiki/ChromeDriver, says it expects the driver to be under: /usr/bin/google-chrome
I'm using dreamhost, and they told me I need to pay more to sudo /usr/bin folder. Anyway to walk around those? (namely, execute chromedriver from random location)
I tried the following (found from anther question about the same topic but no exact answer)
chromedriver = "path/to/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
browser = webdriver.Chrome(chromedriver)
But it doesn't work. It gave me error: cannot find Chrome binary
Seems like another problem:(
Thanks ahead, it's gonna be very helpful! Btw I'm using Python/Flask
You can pass chromedriver location using executable_path variable. Please find below sample code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
driver.get("https://code.google.com/p/chromedriver/issues/detail?id=1260")
You need to add the location of chrome driver to your PATH.
PATH=$PATH:[random folder path]
after that you should be able to run chromedriver and get something like:
Starting ChromeDriver 2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b) on port 9515
Only local connections are allowed.

Categories

Resources