How to make chrome tab executed by selenium/chromedriver to not close - python

How does one make the google chrome tab to not close immediately after its opened.
Could use time.sleep or use a while loop in some type of way?
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get("https://www.google.com")

Found it myself.
Use this
chrome_options.add_experimental_option("detach", True)

Related

Chrome browser automatically closing just after 1 sec,, what would be the reason

from selenium import webdriver
driver= webdriver.Chrome(executable_path="C:\Drivers\chromedriver_win32\chromedriver.exe")
driver.get("https://opensource-demo.orangehrmlive.com")
just opens the chrome and closes automatically after 1 sec
what is the reason? is this any error with code?
Try using the detach option when initializing the chromedriver. Also, executable_path is deprecated now. Service should be used instead, as following:
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
webdriver_service = Service('C:\Drivers\chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome(options=chrome_options, service=webdriver_service)

How to load this dynamic website with Selenium

Trying to load this dynamic website but failed. Anyone can help?
driver = webdriver.Chrome()
driver.get('https://apply.95559.com.cn/personbank/portals/ptQuotaQryCoins.do')
Not that super clear about the issue you are facing while loading the dynamic website. However I was able to load the website using the following code block:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://apply.95559.com.cn/personbank/portals/ptQuotaQryCoins.do')
Browser Snapshot:

Excel File is not downloaded using selenium we driver. Element is clicked but file isn't getting downloaded

I tried to download one excel file using the python selenium web-driver. After running the code I noticed that file is not downloaded (may be a source restriction). I am unable to figure out whether this can be handled or not. below is my code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
ChromeDriver_Path = ChromeDriverManager().install()
options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument('window-size=1920x1080')
Source_Export_Path = r'myfolderpath'
options.add_experimental_option("prefs", {"download.default_directory": Source_Export_Path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
Source_Url = 'https://www.rbnz.govt.nz/statistics/j10-insurance-income-statement?__cf_chl_jschl_tk__=GXpMiuooASSLEEV4GrC0ODNHt3tzhq3PE2pXI_hoUsw-1641891789-0-gaNycGzNC6U'
driver = webdriver.Chrome(executable_path= ChromeDriver_Path, options = options)
driver.get(Source_Url)
element_1 = driver.find_element_by_css_selector(
'#RbnzContent > div.table.summaryinfo-table > div:nth-child(5) > div.summaryinfo-data.col-lg-8.col-md-9.col-sm-12.col-xs-12 > a:nth-child(2)'
).click()
but when I check my directory I see that there are no files downloaded. I even checked the selenium browser window and I noticed that website isn't letting python script to download the file using automated way. Is there any way to fix this issue!!.
This is the url:
try adding a delay / wait before clicking the element.
Expected conditions explicit wait is the preferred way to do that.
Also, you could improve the locator.
Also in case the code you shared is all your code you can add a delay after the clicking on the download button otherwise the session will close immediately after the click so file will be not downloaded.
Let me know if this worked better:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
ChromeDriver_Path = ChromeDriverManager().install()
options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument('window-size=1920x1080')
Source_Export_Path = r'myfolderpath'
options.add_experimental_option("prefs", {"download.default_directory": Source_Export_Path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
Source_Url = 'https://www.rbnz.govt.nz/statistics/j10-insurance-income-statement?__cf_chl_jschl_tk__=GXpMiuooASSLEEV4GrC0ODNHt3tzhq3PE2pXI_hoUsw-1641891789-0-gaNycGzNC6U'
driver = webdriver.Chrome(executable_path= ChromeDriver_Path, options = options)
wait = WebDriverWait(driver, 20)
driver.get(Source_Url)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(#href,'Statistics') and(contains(text(),'Insurance'))]"))).click()
time.sleep(10)

Selenium chromedriver how disable translation bar in headless mode

When opening some web pages in Selenium using Chrome driver I found that chrome shows the translation bar in headless mode, but it doesn't show it when headless is disabled.
Example (headless enabled)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.headless = True
chrome_options.page_load_strategy = 'normal'
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://blog.bellostes.com/?attachment_id=5392')
driver.save_screenshot('headless_true.png')
driver.quit()
Result:
Example (headless disabled)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.headless = False
chrome_options.page_load_strategy = 'normal'
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://blog.bellostes.com/?attachment_id=5392')
driver.save_screenshot('headless_false.png')
driver.quit()
Result:
What I have already tried:
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--disable-features=TranslateUI')
chrome_options.add_argument('--disable-translate') # Not supported any more on Chrome
No difference. Is any I can do to get rid of the translation bar in headless mode?

Disabling Cookies in Webdriver for Chrome/Firefox

I am trying to disable all cookies when starting up either the Chrome or Firefox browser. I have seen the examples on here but they're all in Java, and some of the Selenium code is different than it is for Python.
ChromeOptions options = new ChromeOptions();
Map prefs = new HashMap();
prefs.put("profile.default_content_settings.cookies", 2);
options.setExperimentalOptions("prefs", prefs);
driver = new ChromeDriver(options);
I want to do the above, just in Python.
For Firefox:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference("network.cookie.cookieBehavior", 2)
browser = webdriver.Firefox(firefox_profile=fp)
Source: the FAQ, a JS selenium cookie question, and the description of Network.cookie.cookieBehavior.
For Chrome after version 45, you would need to do this (#alecxe was right up til Chrome 45 I think):
selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 2})
driver = webdriver.Chrome(chrome_options=chrome_options)
The only meaningful change there is default_content_settings becomes default_content_setting_values.
It would be:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})
driver = webdriver.Chrome(chrome_options=chrome_options)
tested - worked for me (Chrome 45, selenium 2.47).
You only need to change there is {"profile.default_content_setting_values.cookies": 2} becomes {"profile.block_third_party_cookies": True}.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.block_third_party_cookies": True})
driver = webdriver.Chrome(chrome_options=chrome_options)

Categories