Keep Firefox from creating dialog boxes - python

I currently have a script that will log on to my company's wiki, visit a page, and select a download to pdf option available on the page. However, when this option is chosen, this dialogue box
I have read there is a way to create a Firefox profile that will suppress the creation of dialogue boxes, but I am unfamiliar with the library.
from splinter import Browser
browser = Browser()
browser.visit('https://company.wiki.com')
browser.find_by_id('login-link').click()
browser.fill('os_username', 'user')
browser.fill('os_password', 'pass')
browser.find_by_name('login').click()
browser.visit('https://pageoncompany.wiki.com')
browser.find_by_xpath('//*[#id="navigation"]/ul/li[4]').click()
browser.find_by_id('action-export-pdf-link').click()

I was able to set the preference in the Firefox Browser, then call my firefox profile
browser = Browser('firefox', profile=r'C:\Users\craab\AppData\Roaming\Mozilla\Firefox\Profiles\0lot9hun.default')

Related

how do I acces whatsapp web with selenium in python?

So I am using geckodriver.exe (for Firefox), and I use the following code to acces whatsapp web:
from selenium import webdriver
browser = None
def init():
browser = webdriver.Firefox(executable_path=r"C:/Users/Pascal/Desktop/geckodriver.exe")
browser.get("https://web.whatsapp.com/")
init()
But everytime I rerun the code, the QR-Code from whatsappweb has to be scanned again and I dont want that. In my normal chrome browser I dont have to scan the QR-Code everytime. How can I fix this ?
Since every time you close your selenium driver/browser, the cookies that attached with the session will also be deleted. So to restore the cookies you haved saved, you can retrieve it after the end of the session and restore it in the beginning of the next.
For getting the cookies,
# Go to the correct domain, i.e. your Whatsapp web
browser.get("https://www.example.com")
# get all the cookies from this domain
cookies = browser.get_cookies()
# store it somewhere, maybe a text file
For restoring the cookies
# Go to the correct domain, i.e. your Whatsapp web
browser.get("https://www.example.com")
# get back the cookies
cookies = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
browser.add_cookies(cookies)
What you could do is define a profile in Firefox. Then open firefox with that profile and open web.whatsapp.com. You will be prompted with the QR code. You link that instance. From there you can use the newly created profile in Python.
Creating a new profile can be done by typing about:profiles in the url section of Firefox:
Then open the browser by clicking 'Launch profile in new browser':
In your Python code you create a reference to this profile:
options.add_argument('-profile')
options.add_argument('/home/odroid/Documents/PythonProfile')
A step by step guide can also be found here.

Alternatives to selenium for automating data input?

I am currently using selenium to automate the input of data in to a website. The website never changes, and the fields are always the same with obviously the data differing.
How I want it to work is for the user to already be logged in to the website, they run a script and a new tab opens in their current browser session with the relevant fields having the data in them.
At the moment it opens a new Chrome session (ignoring the login from the previous session), has to log-in to the site, open a new tab, go to the data input page and push the keys from there. This can be a time consuming activity, and I don't like how it has to login each time. Snippet of my code below.
req = request.get_json()
jsonify(req)
url1 = "www.loginpage.com"
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get(url1)
u = driver.find_element_by_id('username')
u.send_keys("username")
u = driver.find_element_by_id('password')
u.send_keys("password")
u = driver.find_element_by_id('loginButton').submit()
driver.execute_script('''window.open("www.datainputpage.com","_blank");''')
driver.switch_to_window(driver.window_handles[1])
driver.find_element_by_id('Field1').send_keys(req[0])
driver.find_element_by_id('Field2').send_keys(req[1])
driver.find_element_by_id('Field3').send_keys(req[2])
driver.find_element_by_id('Field4').send_keys(req[3])
Is there a way using python I can automate it as mentioned? Opens new tab in current session - fills in fields?
You can use profiles in Chrome. You specify the directory of your profile and all cookies and stuff will be saved in there. So the next time you run it, it should load those same cookies from your previous session and stay logged in.
chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(executable_path=r'chromedriver.exe', chrome_options=chrome_options)
Another possible option is saving the cookies to a json file, then on the next run, load them and set them in the browser.
Selenium Cookies
Reading & Writing JSON

Firefox Browser Extensions not loading: Selenium with Python 2.7

Using Firefox 54, Python 2.7 and Selenium 3.8.1 I'm trying to load some extensions using a Firefox Profile but Selenium fails to load them. The browser, however, does load with this code:
from selenium import webdriver
def setup_firefox_profile():
firefox_custom_profile = webdriver.FirefoxProfile(
r'C:\Users\Owner\AppData\Roaming\Mozilla\Firefox\Profiles\1hmz9w1k.GenericFootprintHardened2')
firefox_custom_profile.add_extension(r'C:\Users\Owner\AppData\Roaming\Mozilla\Firefox\Profiles\1hmz9w1k.GenericFootprintHardened2\extensions\bestproxyswitcher#bestproxyswitcher.com.xpi')
# C:\Users\Owner\AppData\Roaming\Mozilla\Firefox\Profiles\<profile folder>
# Adblock, Best Proxy Switcher, //BetterPrivacy, Clear Flash Cookies, Canvas Fingerprint Blocker, Cookie Quick Manager, Disable WebRTC, Ghostery, Privacy Badger,
firefox_custom_profile.set_preference("extensions.bestproxyswitcher.currentVersion", "5.4.2")
firefox_settings = webdriver.Firefox(firefox_profile=firefox_custom_profile)
return firefox_settings
driver = setup_firefox_profile()
driver.get('http://duckduckgo.com')
Attempting to load the profile with a manual session I am able to see the extensions load.
It appears the profile does not load correctly. I found instructions to go to "about:support" and then "Open Folder" under the Profile Folder heading. The directory did not match the profile in name or path ("C:\Users\Owner\AppData\Local\Temp\tmpp4lzxe")
Can anyone see why the profile is not being loaded?
Is the code for loading the extensions also wrong?

Python selenium get redirected url with Phantomjs

Here is my problem: I'm trying to use selenium to access a webpage and the special about this page is it is an auto redirecting page (you open that page and after few seconds, it automatically redirect to another page). When i use driver = webdriver.Firefox(), my IDM catched that link just perfectly after few seconds.
And because i don't want the browser to come up so i use Phantomjs instead, ut it not working. My application just can get the loading page url (bitdl-1336...) but not the redirected link. Please help!
This is my code:
link = 'http://torrent.ajee.sh/hash.php?hash=' + self.global_hash_code
driver = webdriver.PhantomJS('phantomjs.exe')
driver.get(str(link))
element = driver.find_element_by_link_text('Download Zip')
element.click()
time.sleep(10)
msg = QMessageBox.information(self, QString('Thành công'),QString(driver.current_url))
And this is the result:
Please help!
Sorry about my english
Not exactly an answer to your PhantomJS-specific question, but a workaround to the problem.
And because i don't want the browser to come up so i use Phantomjs instead
You can continue using Firefox, but start it in a Virtual Display, see more information at:
How do I run Selenium in Xvfb?
You may also need to let the browser automatically save the archive in a specified directory, see:
How do I automatically download files from a pop up dialog using selenium-python
Access to file download dialog in Firefox

Clicking on a Javascript Link on Firefox with Selenium

I am trying to get some comments off the car blog, Jalopnik. It doesn't come with the web page initially, instead the comments get retrieved with some Javascript. You only get the featured comments. I need all the comments so I would click "All" (between "Featured" and "Start a New Discussion") and get them.
To automate this, I tried learning Selenium. I modified their script from Pypi, guessing the code for clicking a link was link.click() and link = broswer.find_element_byxpath(...). It doesn't look liek the "All" button (displaying all comments) was pressed.
Ultimately I'd like to download the HTML of that version to parse.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers/") # Load page
time.sleep(0.2)
link = browser.find_element_by_xpath("//a[#class='tc cn_showall']")
link.click()
browser.save_screenshot('screenie.png')
browser.close()
Using Firefox with the Firebug plugin, I browsed to http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers.
I then opened the Firebug console and clicked on ALL; it obligingly showed a single AJAX call to http://jalopnik.com/index.php?op=threadlist&post_id=5912009&mode=all&page=0&repliesmode=hide&nouser=true&selected_thread=null
Opening that url in a new window gets me the comment feed you are seeking.
More generally, if you substitute the appropriate article-ID into that url, you should be able to automate the process without Selenium.

Categories

Resources