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
Related
When I run this code, the page opens and closes. I can't reach the page. Everything is in the latest version. I am a Windows user.
I was trying to open Instagram page with this code.
enter image description here
I tried to open the instagram site with this code and the site was closed as soon as it was opened.
Im not familiar with selenium but I know for a fact that
get() just gets the HTML code of the website and does not display it
You have to add the below Chrome Option:
options.add_experimental_option("detach", True)
Full code:
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(chrom_driver_pat), options=options)
driver.get(<url>)
Next time, don't post the image, post the code and explain your issue clearly.
First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.
Selenium now uses Service method to run webdriver.
To run what you want, you need to fix some parts of your code, like this:
from selenium import webdriver
# in my case, i'm using selenium==4.5.0, for this version, is necessary to
# use selenium.webdriver.chrome.service to use driver path
from selenium.webdriver.chrome.service import Service as ChromeService
# For Windows path, use r"C:\..." when you will use \
# Remember to set the executable file too
chrome_driver_path = r"C:\pythondriver\chromedriver\chromedriver.exe"
# you can use "universal" path too, like linux, using / instead of \
# chrome_driver_path = "C:/pythondriver/chromedriver/chromedriver.exe"
# instead using:
# webdriver.Chrome()
# driver = webdriver.Chrome(chrome_driver_path)
# use this:
chrome_service = ChromeService(chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service)
url = "https://instagram.com"
driver.get(url)
I tested using the configurations below and worked for me:
Chromedriver - 108.0.5359.71
Chrome - 108.0.5359.125 - 64 bits
If you need more help using Chrome Service, try look the Selenium Docs
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()
i am trying to control my browser using python, what I need is I give commands in terminal that should work on the browser like opening and searching for something(like scorling the bowser) and closing the browser
currently I am done with opening the browser and closing
yes, you can by using python selenium driver.
This is simple code to test. Also don't forget to download selenium driver.
https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Read docs in link
This is achievable (at least in POSIX systems like Linux or BSD) by using pipes.
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"
I'm trying to teach myself some python and I've been working on a project which uses selenium to open firefox and interact with netflix but I need to enable 'Play DRM' to stream, I dont see it in 'set_preferences' and I cant inspect element on the contents page in preferences so I'm not sure how to enable it.
import time, pyautogui, os
from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys
profile = wd.FirefoxProfile()
ntfx='http://www.netflix.com/'
driver=wd.Firefox()
driver.get(ntfx)
url = driver.current_url
Took 4 days but finally came with a solution
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addPreference("media.eme.enabled",true);
firefoxOptions.addPreference("media.gmp-manager.updateEnabled", true);
Then add the firefoxOptions to the driver
Ok looked more into the Firefox Profiles and made a custom profile as show here. I went into preferences/content and enabled "Play DRM" and added the PATH to the custom profile
profile = wd.FirefoxProfile('./fire_fox_profile')
driver=wd.Firefox(profile)
As alternative solution of using firefox profiles you can do
driver.get('about:preferences')
driver.find_element_by_id('playDRMContent').click()
The method of #RonanB worked for me
Just go into about:profiles in firefox
then under the DRM enabled profile, look for the root directory. For me it was : /home/myusername/.mozilla/firefox/14iw27z4.default-release
so like this :
myProfile = webdriver.FirefoxProfile('/home/myusername/.mozilla/firefox/14iw27z4.default-release')
driver = webdriver.Firefox(myProfile)
First time answering any question on stackoverflow
This little trick worked for me so what i did was
First make sure normal firefox(i.e when not launched using selenium or firefox) is able to play drm video.
Next, i created new firefox profile named 'selenium' and launched firefox with selenium profile.
To create firefox profile run 'firefox -p' command in windows run program. picture for ref
Now in 'selenium' profile firefox session go to drm video site. Firefox downloaded some drm playing content and after some time drm video played.
then i launched firefox with this code:-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile(r"C:\Users{USER}----PATH TO SELENIUM PROFILE FOLDER----")
print('lll')
driver = webdriver.Firefox(profile, executable_path=r'-------------\firefox\geckodriver.exe')
url = 'http://amp.azure.net/libs/amp/latest/samples/videotag_multiDRM_PlayReadyWidevineFairPlay_notoken.html'
driver.get(url)
and my drm video played,
Now this worked for me not sure will work for you
tested on:-
Firefox Version 92.0(64-bit)
gechodriver version geckodriver-v0.29.1-win64