Starting a browser in selenium - python

I want to run tests with selenium. IE gives me a modal error after bringing up IE 8 with this text "This is the initial start page for the WebDriver server" :
from selenium import webdriver
import time
browser = webdriver.Ie() # Get local session of IE
browser.get("http://www.google.com") # Load page
time.sleep(5)
browser.close()
So I tried Chrome.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.google.com")
time.sleep(5)
browser.close()
and Selenium errors for not having the right path to the chrome.exe application. Chrome is installed as expected... C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
A little help here would be greatly appreciated.

Have u downloaded the Chrome Driver?
To get set up, first download the appropriate prebuilt server. Make sure the server can be located on your PATH or specify its location via the webdriver.chrome.driver system property.
Then when u run
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.google.com")
time.sleep(5)
browser.close()
It should work.

Related

Selenium Chrome Driver not able load website

Chromedriver is launched correctly, then start loading the page, the URL changes to 'http://app1.nmpa.gov.cn/?CbSlDlH0=qGk8rqrP3bxP3bxP39Exb4.QiGYTsZaK4uvCaZ_lRVZqqDE' however the page remains blank ( white).
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path="D:\Python\chromedriver.exe")
driver.maximize_window()
url = 'http://app1.nmpa.gov.cn/'
driver.get(url)
wait = WebDriverWait(driver, 60)
time.sleep(5)
driver.quit()
I tried it, I get redirected to the page "http://app1.nmpa.gov.cn/data_nmpa/face3/dir.html" which looks like this:
I did it with the firefox browser and geckodriver. Can you try and see if the problem still exists?

Python Selenium - Clearing chrome cache

I'm trying to clear the cache from the chrome driver with selenium.
The below code worked fine for a day, and now it has stopped working for some reason. It redirects to the ClearBrowserData url, but it does not press enter to run the Clear Data button.
Am I doing something wrong? I would appreciate some help on this.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = 'path'
browser = webdriver.Chrome(executable_path = chromedriver)
browser.get('chrome://settings/clearBrowserData')
browser.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
beacuse shadowRoot
<settings-clear-browsing-data-dialog>
#shadow-root (open)
</settings-clear-browsing-data-dialog>

Chrome opens with “Data;” with selenium chromedriver

Trying to open "Google" or any other page (website) from Chrome via selenium chrome driver in python.
The code is :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome()
driver.get('https://google.com')
However, this opens my chrome window with the specified link and "data;" tab.
Why that data; tab opens? How to fix it?
Using latest versions of Chrome and Chromedriver
You don't need as much module for this just remove all of those apart from:
from selenium import webdriver
And try again you will not get another tab with congaing data.
import time
time.sleep(1)
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(1)
I'm not sure if its the same problem, but some time ago I made an exe script to run in another PCs and in one of the PCs selenium just didn't worked with Chrome.
This is the question I posted, but the answers didn't help me, hope it works with you: Chromedriver do not open a new session, it opens a new tab in a existing session
If it doesn't work, I made a workaround to run with Firefox instead of Chrome to ensure it would work properly.
With selenium 4 (or newer), you can use the following code to launch a new browser, (without the Chrome is being controlled message), and then the browser navigates to Google in the same tab:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService()
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://google.com')

driver.get not working when opening chrome driver with cookies

I'm trying to use selenium on OSX to open a chrome window with existing cookies so that I can bypass login. When I don't add the argument 'chrome_options=option' to open chrome with my user settings, the driver.get function works fine and it opens a chrome window with no extensions loaded and browses to the url. But when I use the code as shown below, it simply opens chrome (with all extensions loaded) but does not browse to the URL. Am I missing something simple?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/me/Library/Application Support/Google/Chrome/")
driver = webdriver.Chrome("./assets/chromedriver",chrome_options=options)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.gmail.com/")

Using Selenium in the background

I'm using Selenium and chrome webdriver but when I run scripts it opens a window. Is there any way that it can access the internet without the window popping up?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://ps.rsd.edu/public/")
elem = driver.find_element_by_name("account")
elem.send_keys("Username")
elem2 = driver.find_element_by_name("pw")
elem2.send_keys("Password")
elem.send_keys(Keys.RETURN)
driver.quit()
For example, this goes to my school's grade site and puts in a username and password but I want to do this without the browser popping up if that's possible.
I would suggest try using headless PhantomJs GhostDriver (which is a relatively new thing). As this is the native Selenium Webdriver way of doing it.
Download PhantomJs executables from http://phantomjs.org/download.html.
driver = webdriver.PhantomJS("./phantomjs") # path to phantomjs binary
driver.get("https://ps.rsd.edu/public/")
elem = driver.find_element_by_name("account")
elem.send_keys("Username")
elem2 = driver.find_element_by_name("pw")
elem2.send_keys("Password")
elem.send_keys(Keys.RETURN)
driver.quit()

Categories