(Python,Selenium) How to minimize firefox window while running - python-2.7

how to minimize a firefox window by using selenium and python
i tried with
try:
body=None
body = driver.find_element_by_tag_name("body")
body.send_keys("{%+" "+N}")
print "entered keys"
except NoSuchElementException:
print "item body is not exists"
code:2
------
body.send_keys(Keys.CONTROL+Keys.ESCAPE+'D')
code:3
------
body.send_keys("{%" "n}")
Nothing worked for me i want to minimize my firefox window while running or after invoking
or run in invisible mode which has no focus

The following code should help:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.google.com")
actionChain = ActionChains(driver).key_down(Keys.ALT)
actionChain.send_keys(Keys.SPACE)
actionChain.send_keys("n")
actionChain.perform()

Related

Python 2.7 Selenium unable to extract data

I am trying to extra data by return error
NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//*[#id=\'searchpopbox\']"}' ; Stacktrace:
at FirefoxDriver.findElementInternal_ (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/driver_component.js:8444)
at FirefoxDriver.findElement (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/driver_component.js:8453)
at DelayedCommand.executeInternal_/h (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/command_processor.js:10456)
at DelayedCommand.executeInternal_ (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/command_processor.js:10461)
at DelayedCommand.execute/< (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/command_processor.js:10401)
My code is as below and I am trying to get the list from the link
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
browser = webdriver.Firefox(profile)
url = 'https://www.bursamarketplace.com/index.php?tpl=th001_search_ajax'
browser.get(url)
time.sleep(15)
a = browser.find_element_by_xpath("//*[#id='searchpopbox']")
print a
I am seeking your help to get the right xpath for the url.
This gets all the listing for that table.
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.bursamarketplace.com/index.php?tpl=th001_search_ajax")
time.sleep(15)
a = driver.find_element_by_xpath("//*[#id='searchpopbox']")
print(a.text)
Or without chromedrivermanager same thing applies to firefox
.Chrome(executable_path='absolutepathofchromedriver.exe')

How to handle pop up window dialog to download file automatically with firefox profile in python selenium on Linux (Ubuntu) system

I am trying to download the file automatically from system file download dialog by setting preference in firefox profile in my python selenium code , but my code is not working.
Browser : Firefox 72.0
Selenium Version : 3.14
OS : linux Ubuntu
Filetype to download: *.enc (encrypted file type)
Path of firefox in linux : /usr/bin/firefox
Code :
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/user/Downloads/tests")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-uuencoded,application/octet-stream")
self.driver = webdriver.Firefox(firefox_profile=profile)
Hi #Sum i have resolving, my problem was a different Content-Type.
Use this example to resolve your problem , and to understand your Content-Type: https://stackoverflow.com/a/36356422/12911814
In my case the Content-Type was "application/force-download" not "application/pdf"
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.dir", "<path>")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/force-download")
These settings worked for me. Hope it might help you.
I have the same problem with Firefox 72.0, but with pdf files. This is the code:
fp = webdriver.FirefoxProfile()
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", "/path")
fp.set_preference("browser.download.downloadDir", "/path")
fp.set_preference("browser.download.defaultFolder", "/path")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
driver = webdriver.Firefox(firefox_profile=fp)
I have tried all possible preferences, but it always triggers the download alert.
Try this will work like charm ......
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import pyautogui
try :
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
# Click the OK button and close
time.sleep(5)
webelem = driver.find_element_by_id('download')
webelem.click()
time.sleep(5)
print('press enter')
pyautogui.press('enter')
except Exception as err:
print('ERROR: %sn' % str(err))
driver.quit()
The correct MIME type for .enc is "text/x-uuencoded"
Updated as below in code and it's working :
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/x-uuencoded")

chromedriver can't click when running a script, but can in shell

I have a problem in general with clicking in Chromedriver when the code is being ran by Python. This code is used in the script:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver.get("https://www.marktplaats.nl/")
cook_button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//form[#method='post']/input[#type='submit']"))).click()
It just times out giving "NoSuchElementException". But if I put those lines manually in the Shell, it clicks like normal. For what it's worth, I'm using the latest 2.40 Chromedriver and Chrome v67. Running it headless doesn't make any difference.
EDIT
The program actually breaks after on the third command when it tries to find an element that doesn't exist because the click wasn't completed
driver.get(master_link) # get the first page
wait_by_class("search-results-table")
page_2_el = driver.find_element_by_xpath("//span[#id='pagination-pages']/a[contains(#data-ga-track-event, 'gination')]")
So, page_2_el command gives this exception, but only because the click before wasn't completed successfully to remove the warning about cookies.And I'm sure the xpath search is good because it runs with geckodriver in Firefox, but won't do it here with Chromedriver.
EDIT2 See a video of the bug here https://streamable.com/tv7w4 Notice how it flinches a bit, see when it writes on the console "before click" and "after click"
SOLUTION
Replaced
cook_button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//form[#method='post']/input[#type='submit']"))).click()
With
N_click_attempts = 0
while 1:
if N_click_attempts == 10:
print "Something is wrong. "
break
print "Try to click."
N_click_attempts = N_click_attempts+1
try:
cook_button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//form[#method='post']/input[#type='submit']"))).click()
time.sleep(2.0)
except:
time.sleep(2.0)
break
It seems that the click is now completed. I have other clicks in the script and they work fine with element.click(), this one was problematic for some reason.
Your path is correct, but I would suggest a smaller one:
//form/input[2]
And about NoSuchElementException - you can try to add a pause, to wait until element loads and becomes 'visible' for selenium. Like this:
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver.get("https://www.marktplaats.nl/")
cook_button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//form[#method='post']/input[#type='submit']"))).click()
time.sleep(5) # wait 5 seconds until DOM will reload
According edit in the question I would suggest to add time.sleep(5) after clicking on the button. And for the same reason, because after clicking the whole DOM reloads and selenium should wait until reload will be done. On my computer it takes about 2-3 seconds to full reload the DOM.

selenium webdriver tab not switching

selenium webdriver tab swithching not working .
code is :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://www.google.com')
driver.implicitly_wait(2)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
driver.switch_to.window(driver.window_handles[-1])
driver.get('http://www.rediff.com')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
driver.switch_to.window(driver.window_handles[-1])
driver.get('http://www.stackoverflow.com')
driver.switch_to.window(driver.window_handles[0])
In the last line if i change the index from [0] to [1] or [2] there is no change.
Use Keys to have the browser go back to the tab you want. If you want to jump to tab 1 use: driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + '1')

Selenium - chromeDriver u'unknown error : chrome field to start

I'm pretty new in selenium and getting an error with ChromeWebDriver.
I'm using: Chrome 36, ChromeWebDriver 2.10, Windows 7
Here's my code:
from selenium import webdriver
webD = webdriver.Chrome();
But I get the response
unknown error : chrome field to start
How can I fix this?
You may need to download the chrome executable driver from http://chromedriver.storage.googleapis.com/index.html and set the executable path accordingly.
Sample Python Code :
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "./chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#driver = webdriver.Firefox()
driver.get("http://www.python.org")
print driver.title
assert "Python" in driver.title
For more information and end to end script follow
Reference