Selenium from Jupyter Notebook not sending keys - python

From a Jupyter Notebook running Python 3, I'm running the following code to try and insert the text "a search term" into the Google search box with Selenium [1]:
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# get the path of ChromeDriverServer
chrome_driver_path = os.getcwd() + "/chromedriver 2"
# create a new Chrome session
driver = webdriver.Chrome(chrome_driver_path)
# navigate to the application home page
driver.get("https://www.google.com")
# get the search textbox
search_field = driver.find_element_by_name("q")
# enter search keyword and submit
search_field.send_keys("a search term")
However, when I run the code, I just get a new browser with the Google homepage open but the text isn't inserted into the search bar. What am I missing here?
[1] Make sure to have the right webdriver installed -- I initially installed the 32-bit version but then had to install the 64-bit version, since the "2".

The reason was because my 64-bit Chrome Web driver was outdated; the most recent version as of this writing is 2.37 and can be downloaded here: https://chromedriver.storage.googleapis.com/index.html?path=2.37/
Code now works.

Related

How do I get Microsoft Edge to click on SAVE AS with Python

In our company we are using Selenium for web-automation on Windows. Win runs with signed-in user and locked screen. Recently company changed security for not starting downloading automatically. I need to click with Python on Save As button (while Windows is locked => pyautogui is not a option). What about pywinauto or other lib? thx
If you can still use Selenium, try this out:
NOTE - Tested using Windows 11, Edge 97, Python 3.9, and Selenium 4.1
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
# 'executable_path' is deprecated. Use 'service'
driver = webdriver.Edge(
service=Service("C:\\Users\\kabarto\\Documents\\webdrivers\\msedgedriver.exe"))
driver.get("https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/")
# Find the link and use .click to save to Downloads
driver.find_element(
By.XPATH,
"//a[#href=\"https://msedgedriver.azureedge.net/97.0.1072.62/edgedriver_win64.zip\"]"
).click()
# Close browser when download is complete
while not os.path.exists("C:\\Users\\kabarto\\Downloads\\edgedriver_win64.zip"):
time.sleep(1)
driver.quit()
You can also get a list of all the links, and then go through them to find the one you want:
links = driver.find_elements(By.TAG_NAME, "a")
for l in links:
if "win64" in l.get_attribute("href"):
print(l.get_attribute("href"))
I figured it out (work around):
Before downloading, to open a new tab with Edge Downloads
To launch downloading (getting pop-up)
To active tab with Downloads and with Selenium click on particular option (Cancel Save Save As ....)

selenium firefox driver no results returned

I am using VSC Editor, Windows 10, the latest version of Python and all associated plugins.
The browser I am using is FireFox (latest version as well).
When I run my code, a headless browser appears and I can see that my search criteria is entered in to the text box, the search button hit and the results are displayed in the browser.
However, in my code, when I print out the results, I get an empty list [].
What am I doing wrong?
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://duckduckgo.com")
search_form = driver.find_element_by_id("search_form_input_homepage")
search_form.clear()
search_form.send_keys("python jobs remote")
search_form.submit()
results = driver.find_elements_by_class_name("result")
print(results[0])
driver.close()

python requests download trough download button on page

So I am creating an application were you can download files trough a link. This webpage contains a download button and that needs to be pressed in order to start the download. This is the link where you can reference to: link. My code:
link = input("enter link: ")
r = requests.get(link, allow_redirects=True)
how can I make requests or any other library click on the download button and save this file?
Using selenium:
INSTALLATION
Skip this if you already have selenium installed.
Install Selenium, type the following in your terminal: pip3 install selenium
Now you need a webdriver for Selenium. If you are wanting to use Chrome, firstly type "chrome://version/" in your browser and find the version you are using. Then go to this link and download the appropriate webdriver for your browser. If you are using a different browser, like Firefox for example, just type selenium [your browser] webdriver.
Installation docs
CODE
Now for the code (following code is for Chrome, you would only need to change driver = webdriver.Chrome() if you are using a different webdriver):
from selenium import webdriver #importing webdriver
PATH = "C:/path/to/chromedriver" #webdriver location
driver = webdriver.Chrome(PATH)
link = input("Enter link: ")
driver.get(link) #going to URL
driver.find_element_by_xpath("/html/body/div/main/div[3]/div/div/div/div/div[2]/div[2]/span/button")\
.click() #clicking on the button
I used full xpath to locate the button, but you can use multiple things as seen in the documentation.

How to open URL through default Chrome profile using Python Selenium Webdriver

I am on Mac OS X using selenium with python 3.6.3. Im using this code, but browser Google chrome closes immediately after being launched with selenium I start this code, Google chrome opens new windows with Default profile, but chrome wont open the url google.com.
Whats problem with code? Thanks for the help!
FILE_NAME_PROFILE = '/Users/User/Library/Application Support/Google/Chrome'
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir='+FILE_NAME_PROFILE)
driver = webdriver.Chrome('assets/chromedriver', chrome_options=options)
driver.get("https://google.com")
I am using two arguments and work well in development
"user-data-dir=C:\Users\NameUser\AppData\Local\Google\Chrome\User Data"
"profile-directory=Default"
If you want use another profile (not default) you have to create it and only you have to change the second argument. All profiles are stored in 'User Data' folder
"profile-directory=Profile 1"

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

Categories