Using python selenium for Microsoft edge - python

I am trying to use pythons selenium for Microsoft edge but I keep getting this error:
WebDriverException: Message: unknown error: cannot find Microsoft Edge binary
I downloaded the latest version of the edge driver. Here is my code:
from selenium import webdriver
from selenium.webdriver.remote import webelement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from bs4 import BeautifulSoup
import os
from datetime import datetime
from selenium.webdriver import ActionChains
driver = webdriver.Edge(executable_path = 'C:\\Users\\Downloads\\edgedriver_win32\\msedgedriver.exe')
def get_trulia_estimate(address):
driver.get('https://www.trulia.com/')
print(address)
element = (By.ID, 'homepageSearchBoxTextInput')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(element)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(element)).send_keys(address)
search_button = (By.CSS_SELECTOR, "button[data-auto-test-id='searchButton']")
WebDriverWait(driver, 50).until(EC.element_to_be_clickable(search_button)).click()
time.sleep(3)

This post is quite old now, but hopefully I can help anyone that stumbles upon the same issue in future!
The problem is that you're using the wrong webdriver. Edge exists in two different versions, implemented on two non-interchangeable engines -- Chromium Edge and EdgeHTML (the default version at the time of writing). Each of these two versions has a different webdriver associated with it, with Chromium Edge's being "msedgedriver.exe", and EdgeHTML's being "MicrosoftWebDriver.exe".
You are using the EdgeHTML version of Edge, while trying to run the Chromium Edge webdriver. The 'cannot find Microsoft Edge binary' error Selenium spits out comes from this.
Luckily it is easy to install the right webdriver. If you have a Edge 17 or older, you can install the driver here. Make sure you download the EdgeHTML driver, not the Chromium driver, and add it to your PATH. For Edge 18 and later, you don't have to download anything. Simply run in the command prompt the command: DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0.

WebDriver cannot find your MS Edge path, u can try to uninstall and reinstall Edge.
If its not gonna help add Edge location to your system path or use --binary argument.

The Answer by James L is perfectly summarized. I have Microsoft EdgeHTML 18.17763 and I tried to, therefore, run the command:
DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0
This executed successfully. However, when running my code this time around, I get the error:
Message=A exception with a null response was thrown sending an HTTP
request to the remote WebDriver server for URL
http://localhost:52109/session. The status of the exception was
ReceiveFailure, and the message was: The underlying connection was
closed: An unexpected error occurred on a receive.
Looks like we need to, additionally, also enable developer options in Windows>>Settings>>Developer Options, which, since I do not have admin privileges, I am currently unable to do.

You must install msedge driver as You do with Chromedriver.
You first check wich version your edge is and then download de Edge driver from Microsoft Page
Download and unzip the msedgedriver file according your browser version
Install the package of it to python in terminal (pip install msedge-selenium-tools selenium==3.141)
Finally try the code to get the browser:
from selenium import webdriver
edgeBrowser = webdriver.Edge(r"C:....\msedgedriver.exe")
edgeBrowser.get('https://www.google.com')

Related

Python selenium - AttributeError: 'dict' object has no attribute 'get_attribute'

I wanted to showcase selenium and instead of starting a complete Java / C# suite I wanted to quickly use Python since it can be done with a single script.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
print("sample test case started")
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("MyUrl")
element = driver.find_element(By.ID, "Username")
element.send_keys("MyUsername")
driver.close()
print("sample test case successfully completed")
I am getting this error message:
AttributeError: 'dict' object has no attribute 'send_keys'
I would seam that find_element method is returning some kind of dict (Dictonary??) object instead of a selenium web element. How can I fix this??
I ran into this exact issue. It seems to be some sort of bug in Selenium 4.0.
I have reverted to 3.141.0 (pip install selenium==3.141.0) and it seems to have resolved this issue.
Selenium project is moving to only support W3C and Chrome (since version 75) is using W3C by default. You can run into an issue that you described, if you have old version of chromedriver and selenium >=4, which was the problem in my case. Update your chromedriver or downgrade Selenium version to <4 and it should fix your problem.
I reproduced this problem using Selenium 4.1.0 and Chromedriver 98, by disabling w3c:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("w3c", False)
driver = webdriver.Chrome(options=options)
So, it's another option that could lead to this error.
You can fix it by removing it or changing its value to True.

'unresolved reference Error' 'from webdriver_manager.chrome import ChromeDriverManager

I am creating an application that shall interact with websites, the important code is listed below. So when it comes to importing 'ChromeDriverManager' from 'webdriver_manager.chrome' I get the Error "unresolved reference 'webdriver_manager'" and "unresolved reference'ChromeDriverManager'". The problem is that I want to autoupdate the chromedriver and this was the solution I came up with but it won't work when running it in pycharm but when I run it from the console it would work...does anyone have a solution on this because I prefer to test out my code in pycharm than starting it from the console every time.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager # important for chromedriver-autoinstall
import time
p= ChromeDriverManager()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
# disables 'Browser is managed by test software' and surpresses error with USB device
driver = webdriver.Chrome(executable_path=p.install(), options=chrome_options)
# see above + install right version of chromedriver
t = 2 # time to wait for input of number
i = 1 # current round
duration = 1
Had the same issue, as the package didn`t show up in the interpreter. I ended up just installing the webdriver-manager manually via settings.
Package installation interface

Selenium web driver Firefox opening blank page

I'd like to ask something about Selenium library in Python.
I'm trying to open a webpage, directly log onto it, and access another webpage behind it (I wanted to navigate on the website after the login) with a Python script. I've found the following code on the Internet but I have a problem with the line:
browser = webdriver.Firefox()
It just opens a blank page in Firefox and it looks like the script get stuck with it and does nothing afterwards. I tried in the Python interpreter and it's the same, it opens a blank page in Firefox and I lose the hand (I can't enter other commands).
python interpreter blocked:
I'm using Selenium-3.3.1 and I work under CentOS 6.5.
Is it normal? Am I missing something obvious?
Here is my code:
#!usr/bash/python
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
def loadedPage(browser):
return browser.find_element_by_tag_name("body") != None
browser = webdriver.Firefox() #supposedly just a firefox webdrive instance creation
browser.get("http://machine/machineDir/index.php")
wait = ui.WebDriverWait(browser, 10)
wait.until(loadedPage)
username=browser.find_element_by_id("username")
username.send_keys("userTest")
passwd=browser.find_element_by_id("password")
passwd.send_keys("userTestpass")
passwd.send_keys(Keys.RETURN)
As you are using selenium 3, firefox browser can't be instantiate directly, you need to configure gecko driver for the same.
System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
I fixed it using the right version of Selenium for my old Firefox.
Firefox version: 17.0.10
Selenium version installed: 2.40

Python, error with web driver (Selenium)

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)
I am getting the error:
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
I am not sure whythis is happening, because I imported selenium already.
Either you provide the ChromeDriver path in webdriver.Chrome or provide the path variable
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driverLocation = 'D:\Drivers\chromedriver.exe' #if windows
driver = webdriver.Chrome(driverLocation)
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)
Best way to eliminate this Exception without altering the code eevn single line is to add the chromedriver.exe( or nay other browser driver files) in to Python
site_packages/scripts directory for windows
dist_package/scripts for Linux
Please check this solution, it works.
If you are using a Mac, then don't include '.exe' I put the selenium package directly into my Pycharm project that I called 'SpeechRecognition'. Then in the selenium file, navigate to: /selenium/webdriver/chrome, then copy and paste the 'chromedriver.exe' file you downloaded most likely from [here][1]
Try this script if you are using PyCharm IDE or similar. This should open a new Google window for you.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome('/Users/Name/PycharmProjects/SpeechRecognition/selenium/webdriver/chrome/chromedriver')
browser.get('http://www.google.com')
Then if you want to automatically search an item on Google, add these lines below and run. You should see an automatic google search window opening up. It might disappear quickly but to stop that, you can simply add a while loop if you want or a timer
search = browser.find_element_by_name('q')
search.send_keys('How do I search an item on Google?')
search.send_keys(Keys.RETURN)
[1]: https://sites.google.com/a/chromium.org/chromedriver/home

How to use Selenium with Python?

How do I set up Selenium to work with Python? I just want to write/export scripts in Python, and then run them. Are there any resources for that? I tried googling, but the stuff I found was either referring to an outdated version of Selenium (RC), or an outdated version of Python.
You mean Selenium WebDriver?
Huh....
Prerequisite: Install Python based on your OS
Install with following command
pip install -U selenium
And use this module in your code
from selenium import webdriver
You can also use many of the following as required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
Here is an updated answer
I would recommend you to run script without IDE... Here is my approach
USE IDE to find xpath of object / element
And use find_element_by_xpath().click()
An example below shows login page automation
#ScriptName : Login.py
#---------------------
from selenium import webdriver
#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
baseurl = "http://www.mywebsite.com/login.php"
username = "admin"
password = "admin"
xpaths = { 'usernameTxtBox' : "//input[#name='username']",
'passwordTxtBox' : "//input[#name='password']",
'submitButton' : "//input[#name='login']"
}
mydriver = webdriver.Firefox()
mydriver.get(baseurl)
mydriver.maximize_window()
#Clear Username TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()
#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)
#Clear Password TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()
#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()
There is an another way that you can find xpath of any object -
Install Firebug and Firepath addons in firefox
Open URL in Firefox
Press F12 to open Firepath developer instance
Select Firepath in below browser pane and chose select by "xpath"
Move cursor of the mouse to element on webpage
in the xpath textbox you will get xpath of an object/element.
Copy Paste xpath to the script.
Run script -
python Login.py
You can also use a CSS selector instead of xpath. CSS selectors are slightly faster than xpath in most cases, and are usually preferred over xpath (if there isn't an ID attribute on the elements you're interacting with).
Firepath can also capture the object's locator as a CSS selector if you move your cursor to the object. You'll have to update your code to use the equivalent find by CSS selector method instead -
find_element_by_css_selector(css_selector)
There are a lot of sources for selenium - here is good one for simple use Selenium, and here is a example snippet too Selenium Examples
You can find a lot of good sources to use selenium, it's not too hard to get it set up and start using it.
You just need to get selenium package imported, that you can do from command prompt using the command
pip install selenium
When you have to use it in any IDE just import this package, no other documentation required to be imported
For Eg :
import selenium
print(selenium.__filepath__)
This is just a general command you may use in starting to check the filepath of selenium

Categories