Python: How to automate 'Allow' flash player content in Firefox? - python

I need to allow flash content in an automated manner in Python. I tried using Selenium in Python to do so but could not manage. The issue is that browsers stopped supporting settings that always allow flash content. Moreover, the "Allow" button cannot be accessed via Selenium for instance because it is not a part of the website or a setting in Firefox. Does anyone know about a potential workaround?
Here is an image of the Firefox message that I need to somehow access:

"...The Allow button cannot be accessed via Selenium for instance because it is not a part of the website or a setting in Firefox. Does anyone know about a potential workaround?"
I don't know your OS but if it was my problem...
Try to find a "key press" module to send the A key press into Firefox (ie: the Allow shortcut).
Try to send a mouse-click at the coordinates of Allow button.
A good option to try is pyautogui. Once Flash is enabled by such module (clicker or presser) then you can involve Selenium for whatever you needed to do in the enabled Flash.

To allow flash content in an automated manner using Selenium through Python you need to use an instance of FirefoxProfile() and set_preference() method to configure:
dom.ipc.plugins.enabled.libflashplayer.so to true
plugin.state.flash to 2
Code Block:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("dom.ipc.plugins.enabled.libflashplayer.so","true")
profile.set_preference("plugin.state.flash", 2)
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()

Related

How do you stay logged in with the Selenium Chrome browser?

I use the following code to open a Chrome window using Selenium:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/")
However, whenever I enter the site, I am logged out. On other sites, I can manually do so, but Google deems it unsafe to log in with Selenium.
I know that Selenium is a test browser, but is it possible to stay logged in with Selenium so that I can access my user data; or is that not the purpose of Selenium, and calling the Chrome subprocess the way to go?
To start with Selenium isn't a browser as such but a bunch of libraries which helps in simulating Browsing Context.
From the question it's not that clear why you are logged out once you login in to the site. But definitely once you are logged in using Selenium driven ChromeDriver initiated google-chrome Browsing Context you must remain logged in and should be able to access your user data.

How do I input information into a website with python?

I have this python code, which accesses a website using the module webbrowser:
import webbrowser
webbrowser.open('kahoot.it')
How could I input information into a text box on this website?
I suggest you use Selenium for that matter.
Here is an example code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# driver = webdriver.Firefox() # Use this if you prefer Firefox.
driver = webdriver.Chrome()
driver.get('http://www.google.com/')
search_input = driver.find_elements_by_css_selector('input.gLFyf.gsfi')[0]
search_input.send_keys('some search string' + Keys.RETURN)
You can use Selenium better if you know HTML and CSS well. Knowing Javascript/JQuery may help too.
You need the specific webdriver to run it properly:
GeckoDriver (Firefox)
Chrome
There are other webdrivers available, but one of the previous should be enough for you.
On Windows, you should have the executable on the same folder as your code. On Ubuntu, you should copy the webdriver file to /usr/local/bin/
You can use Selenium not only to input information, but also to a lot of other utilities.
I don't think that's doable with the webbrowser module, I suggest you take a look at Selenium
How to use Selenium with Python?
Depending on how complex (interactive, reliant on scripts, ...) your activity is, you can use requests or, as others have suggested, selenium.
Requests allows you to send and get basic data from websites, you would probably use this when automatically submitting an order form, querying an API, checking if a page has ben updated, ...
Selenium gives you programmatic control of a "normal" browser, this seems better for you specific use-case.
The webbrowser module is actually only (more or less) able to open a browser. You can use this if you want to open a link from inside your application.

Keys.ESCAPE in selenium webdriver(python)

I am using selenium webdriver (with python). I have a use case where I want to test that submit button get disabled once the form get submited. To test it, I send ESCAPE key to stop the page to load next page so that I can access the elements of same page.
password.send_keys("abcdef", Keys.ENTER, Keys.ESCAPE)
The problem is that it works fine in Firefox browser but it is not working in Chrome. In Chrome sending ESCAPE seems to be not working and it submits the form and loads the next page.
Is there any other solution or workaround to overcome this?
After trying many options, finally the following option seems to be working -
password.send_keys("abcdef", Keys.ENTER, Keys.ESCAPE) # this works for Firefox driver
drive.execute_script("window.stop();") # this works for Chrome driver
You can try one of these 3 suggestions from https://sqa.stackexchange.com/a/6208 :
Installing an extension in Chrome called Stop load
using the built in pageLoadTimeout() method in Selenium
using Sikuli or Autoit to access the browser's native controls

How to access popup login form with Selenium in Python

I have been trying for a while to figure out how to enter username and password in the popup-window in this exercise:
http://pentesteracademylab.appspot.com//lab/webapp/digest
but I am entirely new to Selenium in Python. I found out how to click the button, so that the login form pops up:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://pentesteracademylab.appspot.com//lab/webapp/digest")
driver.find_element_by_css_selector('button').click()
but I cannot figure out how to access that window, let alone the fields in it. I have read about switch_to_frame and switch_to_window. For windows there is the window_handles showing you active windows to switch to, but this only returns a single element, which I believe is the main window, not the pop up. I also tried
alert = driver.switch_to_alert()
to no avail. The problem is that I do not know either which kind of object the popup is (frame,window,alert or something else), and I cannot find any names referring to it in the HTML code for the webpage.
Can anyone take me a step further?
Pass the authentication step by accessing the following URL:
http://username:password#pentesteracademylab.appspot.com/lab/webapp/digest/1
See also:
Handling browser level authentication using Selenium

Using Selenium/WebDriver and Python, how do I suppress the prompt to share my camera and microphone?

I am using Selenium in Python to automate a remote browser. The browser needs access to its webcam and microphone. When I navigate to a page that requests access, Firefox shows a pop-up window that asks "Would you like to share you camera and microphone with [host]?"
This window is not part of the browser's page, so it cannot be detected or controlled via Selenium.
This behavior is controlled by the media.navigator.permission.disabled option in Firefox's 'about:config' page. If this option is set to 'true', then access to the camera should be granted automatically.
When I set that option to 'true', it eliminates the prompt only when I run Firefox manually. When I run Firefox via Selenium, I still get the prompt.
How can I suppress this prompt, and have permission granted automatically?
The problem lies in Firefox profiles. Selenium creates a new, temporary profile for each browser instance. This profile is separate from the profile you use when you manually start Firefox.
Thus, when you set media.navigator.permission.disabled to 'true' in about:config, you do so only for your profile, and not for the profile that Selenium uses.
There are two ways to work around this:
Tell Selenium which existing profile to use.
To do this, you must first determine which profile you are using. To do this, close all instances of Firefox, then execute firefox -p to start the profile manager. In most cases, you will see a single profile called default.
Using this profile, navigate to about:config, and set the media.navigator.permission.disabled option to true.
Then, when you start the Selenium standalone server, specify this profile:
java -jar selenium-server-standalone-2.37.0.jar -Dwebdriver.firefox.profile=default
This tells Selenium to use the default profile, which has the settings you want.
Create and configure a new profile for Selenium to use.
Before you create the browser instance, you must create a Firefox profile and configure it to meet your needs:
profile = webdriver.FirefoxProfile()
profile.set_preference ('media.navigator.permission.disabled', True)
profile.update_preferences()
Then specify this profile when you create the remote browser instance:
firefox = selenium.webdriver.remote.webdriver.WebDriver (command_executor=my_url, desired_capabilities=DesiredCapabilities.FIREFOX, browser_profile=profile)
Selenium will then use this profile, and you should not be prompted for permission to access the camera.
Note that this method takes more time than the first method.
You can use options:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("media.navigator.permission.disabled", True)
browser = webdriver.Firefox(options=options)

Categories

Resources