Python Selenium - URL won't open in browser instance - python

This particular URL won't open via python selenium script below. This same code works for most of the urls I have tried it on.
chrome_driver = 'chromedriver.exe' # Change this to your chrome driver path
driver = webdriver.Chrome(chrome_driver)
driver.get(url)
There is no any error message displayed, it will only show blank page and nothing will happen. When manually typed into regular chrome browser, it opens correctly but when manually into chrome browser instance, it won't work just like the script.
What is the solution to fix this?

Related

How to automate secure encrypted sites in Selenium using python?

I'm new to Python. I'm trying to do automation by opening a login page in Selenium.
from selenium import webdriver
browser = webdriver.Chrome(executable_path='chromedriver')
I tried to test some sites like - 'https://www.google.com/',etc. which is working perfectly fine.
url = 'https://www.google.com/'
browser.get(url)
I'm trying to open below url,
url = 'https://yesonline.yesbank.co.in/index.html?module=login'
browser.get(url)
I got the following error in selenium browser while the url is working fine without selenium.
Access Denied
You don't have permission to access
"http://yesonline.yesbank.co.in/index.html?" on this server.
Reference
#18.ef87d317.1625646692.41fe4bc0
But when I'm trying to just open the base url, it is opening but the site gets loads partially and keep showing loading.
url = 'https://yesonline.yesbank.co.in'
browser.get(url)
I feel like I am missing out something while opening the login url which I'm not able to get what exactly.
I also tried changing the webdriver i.e with Firefox.
url = 'https://yesonline.yesbank.co.in'
firefox_browser = webdriver.Firefox()
And guess what, it was opening!
But as soon as I'm trying to get the login page (even by manually using the mouse and clicking login page).
url = 'https://yesonline.yesbank.co.in/index.html?module=login'
firefox_browser.get(url)
'firefox_browser' is getting closed with an session reset error.
Can someone help me how to open secure sites in selenium. Or is there any other way to get it done.
It's finally working with chrome-driver by adding some arguments to it.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
options.add_argument('--disable-blink-features=AutomationControlled')
browser = webdriver.Chrome(executable_path='chromedriver', options = options)

How to open a browser tab after GET and POST requests in python

Im not sure if this is possible, but basically, I'm trying to open a tab in Chrome to show the outcome of my GET and POST requests. For instance, lets say im using Python requests to POST log in data to a site. I would want to then open a chrome tab after that POST request to see if I did it correctly, and also to continue on that webpage from there in Chrome itself. I know I can use response.text to check if my POST request succeeded, but is there a way I can physically open a chrome tab to see the result itself? Im guessing there would be a need to export some sort of cookies as well? Any help or insights would be appreciated.
When using selenium, you need to remove headless option for browser:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('<PATH_TO_CHROMEDRIVER>', options=chrome_options)
# load page via selenium
wd.get("<YOUR_TARGET_URL>")
# don't close browser
# wd.quit()
Also remove closing browser in the end of code.
After that browser will remain open and you will be able to continue work manually.

logging into a website and downloading a file

I would like to log into a website and download a file. I'm using selenium and the chromedriver. Would like to know if there is a better way. It currently opens up a chrome browser window and sends the info. I don't want to see the browser window opened up and the data being sent. Just want to send it and return the data into a variable.
from selenium import webdriver
driver = webdriver.Chrome()
def site_login(URL,ID_username,ID_password,ID_submit,name,pas):
driver.get(URL)
driver.find_element_by_id(ID_username).send_keys(name)
driver.find_element_by_id(ID_password).send_keys(pas)
driver.find_element_by_id(ID_submit).click()
URL = "www.mywebsite.com/login"
ID_username = "name"
ID_password = "password"
ID_submit = "submit"
name = "myemail#mail.com"
pas = "mypassword"
resp=site_login(URL,ID_username,ID_password,ID_submit,name,pas)
You can run chrome in headless mode. In which case, the chrome UI won't show up and still performing the task you were doing. Some article I found on this https://intoli.com/blog/running-selenium-with-headless-chrome/. Hope this helps.
First option: If you are able to change the driver, you can use phantom-js as driver. That was a headless browser and you can use it with selenium.
Second option: If the site are not dynamic (easily called it SPA) or you are able to trace packet (which can be done in chrome dev tools), you can directly use request with the help of beautifulsoup if you need to get some data on the page.
Just add this two lines
chrome_options = Options()
chrome_options.add_argument("--headless")
This should make chrome run in the background.

How to open URL through default Chrome profile using Python Selenium Webdriver

I am on Mac OS X using selenium with python 3.6.3. Im using this code, but browser Google chrome closes immediately after being launched with selenium I start this code, Google chrome opens new windows with Default profile, but chrome wont open the url google.com.
Whats problem with code? Thanks for the help!
FILE_NAME_PROFILE = '/Users/User/Library/Application Support/Google/Chrome'
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir='+FILE_NAME_PROFILE)
driver = webdriver.Chrome('assets/chromedriver', chrome_options=options)
driver.get("https://google.com")
I am using two arguments and work well in development
"user-data-dir=C:\Users\NameUser\AppData\Local\Google\Chrome\User Data"
"profile-directory=Default"
If you want use another profile (not default) you have to create it and only you have to change the second argument. All profiles are stored in 'User Data' folder
"profile-directory=Profile 1"

Selenium Firefox webdriver won't load a blank page after changing Firefox preferences

everyone.
Main question: I am using the Python API for Selenium 2 and want to start a Firefox browser on a blank page (i.e. don't send any requests on browser startup). I created a FirefoxProfile object and changed 'browser.startup.page' to 0. The first time I create a webdriver using this profile it goes to mozilla.org but subsequent webdrivers start on a blank page like I intended. Why is this happening and how can I fix it?
Second question: the code below works fine when I enter it line by line in the interpreter but crashes when I try to run it as a script. I get a WebDriverException: "Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details." I also get a pop up window that says "Your Firefox profile cannot be loaded. It may be missing or inaccessible.". How can I fix this so that it runs as a script?
from selenium import webdriver
profile = webdriver.FirefoxProfile()
# Tell the browser to start on a blank page
profile.set_preference('browser.startup.page', 0)
# Start first session (doesn't work)
driver1 = webdriver.Firefox(profile)
driver1.close()
# Start second session (this works)
driver2 = webdriver.Firefox(profile)
The setting "browser.startup.page" = 0 is the default for webdriver instances. The setting that works for me (old FF defect)
profile.set_preference("browser.startup.homepage_override.mstone", "ignore")
A one-liner workaround without using a profile is to just load the empty page after starting the Firefox instance:
driver = webdriver.Firefox()
driver.get("about:blank")
Oh, I forgot the 2nd part: The error message when running your code in a script is because the old Firefox instance is still running when you start the new one with the same profile. It takes some time to close the old browser window. If you add a sleep of 5 seconds before the last line in your sample, it works from a script too.
It was tricky to see why it worked without this on two of my machines. The reason: Iceweasel and the Firefox of Linux Mint do not show the update page.
BTW: Nice finding, that starting the next Firefox instance will work.

Categories