How to handle SSL Certificate in IE using selenium with python? - python

I'm getting the error as per the image.
Error_img
I tried the following code to solve it.
Method 1 :
from selenium import webdriver
from selenium.webdriver.ie.options import Options
options = Options()
options.set_capability={"acceptInsecureCerts", True}
options.set_capability={"ignoreProtectedModeSettings":True, "ignoreZoomSetting":True}
driver = webdriver.Ie(options=options,executable_path='D:/
Project/Testing/IEDriverServer_Win32_3.150.1/IEDriverServer.exe')
driver.get(url)
options.set_capability={"ie.ensureCleanSession",True}
driver.close()
Method 2:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.INTERNETEXPLORER.copy()
desired_capabilities['acceptInsecureCerts'] = True
driver = webdriver.Ie(capabilities=desired_capabilities,executable_path='E:/DriverServer_Win32_3.150.1/IEDriverServer.exe')
driver.get(url)
print(driver.title)
driver.close()
**Can't share the URL therefore I have just written URL word
I tried both code but it's not working
Is there any another solution ?**

The acceptInsecureCerts capability doesn't work because IE doesn't allow to accept it. You can refer to this link for more detailed information.
In IE 11, you can click the link Go on to the webpage (not recommended) as a workaround to bypass the SSL certificate error. This link has an id "overridelink". You can find the id using F12 dev tools.
I use this site: https://expired.badssl.com/ as an example, the sample code is like below:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
url = "https://expired.badssl.com/"
ieoptions = webdriver.IeOptions()
ieoptions.ignore_protected_mode_settings = True
driver = webdriver.Ie(executable_path='IEDriverServer.exe', options=ieoptions)
driver.get(url)
time.sleep(3)
driver.find_element_by_id('moreInfoContainer').click()
time.sleep(3)
driver.find_element_by_id('overridelink').click()
It works well in IE 11, you can also try the same method.

Related

Wait for cookie with Selenium python webdriver

I basically here the same question as here, only with Python.
I want to use Selenium to get the user to log in with a browser, then grab the session cookie that will contain the log in information, and finally close the browser. In an interactive session, it's easy to just wait until the authentication is complete before calling get_cookie(). But in run mode, I'm stuck.
From the Selenium documentation, I get that it is about defining a wait strategy. So I tried to repurpose their code example as follows:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get('https://examplecom/')
sessionId = WebDriverWait(driver, timeout=120).until(lambda d: d.get_cookie('session_cookie')['value'])
driver.quit()
Unfortunately, the following error is immediately raised:
TypeError: 'NoneType' object is not subscriptable
What should I do instead?
Duh! I was calling the value key as part of the wait function, thus even before it was available! This works:
sessionId = WebDriverWait(driver, timeout=120).until(lambda d: d.get_cookie('TK'))['value']
To save cookies in Selenium, you can do as follows:
import os
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
expanduser = os.path.expanduser('~')
options = webdriver.ChromeOptions()
and then add option of user-data-dir:
options.add_argument(f'--user-data-dir={expanduser}\\AppData\\Local\\Google\\Chrome\\any_Name_You_Want')
and then create your browser with these option:
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)

why is my this simple selenium code isn't working?

Actually it only opens youtube but don't type anything and search
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://youtube.com')
searchArea = driver.find_element_by_xpath('//*[#id="search"]')
searchArea.send_keys('Sujeet Gund')
searchButton = driver.find_element_by_xpath('//*[#id="search-icon-legacy"]')
searchButton.click()
Try //input[#id="search"] instead.
------------------ updated below ------------------
Well, I guess you had copied wrong xpath. Maybe you might just copied the wrapper(or container) not the exact input field.
This solution using specific tag input makes selenium easier to find the element. But be careful not to use when there're more than one element with the same xpath.
Plus, I recommend you to use in this way
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
SERVICE = Service(ChromeDriverManager().install())
OPTIONS = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=SERVICE, options=OPTIONS)
driver.get('https://youtube.com')
searchArea = driver.find_element(by=By.XPATH, value='//input[#id="search"]')
searchArea.send_keys('Sujeet Gund')
because currently some functions in selenium are deprecated.

How to pass cloudfire?

I have a code for making temp mail automatically but I have a problem.
This is code:
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://temp-mail.org/")
time.sleep(10)
browser.close()
The link opens correctly but I can't pass cloudflare.
Also, I see some errors on my console:
Thanks...
Try adding user agent argument in chrome options and set user agent to any value
ops = Options()
ua='me'
ops.add_argument('--user-agent=%s' % ua)
driver=uc.Chrome(executable_path=r"C:\chromedriver.exe",chrome_options=ops)
Alternatively try using undetected-chromedriver
import undetected_chromedriver as uc
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = uc.Chrome(options=options)
driver.get("https://temp-mail.org/")

Capture console.log outputs on Chrome console using selenium

I have a list of websites which I am doing some testing and experimentation on. I visit a website from the list using selenium and inject a piece of JS into some of the files using MITMproxy scripting. This injected code performs some test and output the results using console.log() in JS onto the chrome console, something like
console.log(results of the injected JS)
The injection is successful and the results which I desire do appear on the Chrome Console when I run my experiment. The issue that I am facing is when I try to capture the chrome console for console.log output, it is not successful. It will capture warning and error message from chrome console but not console.log output. Currently this how I am doing it.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
option = Options()
option.add_argument('start-maximized')
# Specify the proxy
option.add_argument('--proxy-server=%s:%s' % (proxy_host, proxy_port))
# enable browser logging
d = DesiredCapabilities.CHROME
# d['loggingPrefs'] = { 'browser':'ALL' }
d['goog:loggingPrefs'] = { 'browser':'ALL' }
# Launch Chrome.
driver = webdriver.Chrome(options=option, executable_path = './chromedriver', desired_capabilities=d, service_args=["--verbose", "--log-path=./js_inject/qc1.log"])
for url in list_urls:
# Navigate to the test page
driver.get(url)
sleep(15)
# in this 15 seconds, the MITMproxy will inject the code and the injected code will output on chrome console.
for entry in driver.get_log('browser'):
print(entry)
Can anyone point me what mistake I might be making or an alternate approach to perform this task. Thank you.
P.S Pardon me for grammatical errors.
options.add_experimental_option('excludeSwitches', ['enable-logging'])
dc = DesiredCapabilities.CHROME
dc["goog:loggingPrefs"] = {"browser":"INFO"}
self.driver = webdriver.Chrome(chrome_options=options, desired_capabilities=dc)
I managed to make it work a while ago, so not sure what exactly did it, but I think it was the experimental option of "anable-logging".

Loading Selenium user profile in Tor Chrome on Windows

This code works for Windows where it launches Chrome connected via Tor. Keep in mind you have to have Tor browser running beforehand. How can I enable the user-profile and start the browser logged in? I have tried the regular method. I have only 1 profile. Default. Doesn't seem to be working. Any clues?
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
tor_proxy = "127.0.0.1:9150"
chrome_options = Options()
'''chrome_options.add_argument("--test-type")'''
chrome_options.add_argument('--ignore-certificate-errors')
'''chrome_options.add_argument('--disable-extensions')'''
chrome_options.add_argument('disable-infobars')
'''chrome_options.add_argument("--incognito")'''
chrome_options.add_argument('--user-data=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
driver = webdriver.Chrome(executable_path='C:\\chromedriver.exe', options=chrome_options)
driver.get('https://www.gmail.com')
time.sleep(4)
driver.switch_to.frame(0)
driver.find_element_by_id("introAgreeButton").click()
Use this instead.
chrome_options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
you don't have to specify the profile directory (Default) as it is used by default if nothing is explicitly specified using below code. SO in your case use only the above line of code
chrome_options.add_argument("profile-directory=Profile 1")
Eg:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"user-data-dir=C:\Users\prave\AppData\Local\Google\Chrome\User Data")
driver =webdriver.Chrome("./chromedriver.exe",options=options)
driver.get(url)
Output:

Categories

Resources