Chromedriver will automatically quit - selenium

from selenium import webdriver
browser = webdriver.Chrome("D:/visualStudioWorkspace/crawling/chromedriver.exe")
browser.maximize_window()
url = "https://flight.naver.com/"
browser.get(url)
when the program is over, chrome driver will automatically quit

Related

Trying to open url in google chrome, but closes immediately after being launched with selenium

I tried to launch chrome with selenium. but as soon as the browser loads the urls, google chrome closes automatically. here is the code:
'''
from selenium import webdriver
url= 'https://www.gmail.com'
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)
'''
Because selenium4 no need to use driver.close()
method they automatically close the driver after execution. So options.add_experimental_option("detach", True) argument will help you to prevent to close immediately after once launch the url with google chrome
# selenium 4
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
#chrome to stay open
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
url= 'https://www.gmail.com'
driver.get(url)
time.sleep(2)

Selenium Chromedriver opening a blank page

When I open this URL with webdriver in selenium, I get a blank page with a 429 request. I haven't sent too many request as I only do one and it doesn't work. I've tried multiple solutions but can't manage to do it. Here is my code:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# to supress the error messages/logs
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("disable-blink-features=AutomationControlled")
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r"C:\\Users\\pople\\OneDrive\\Desktop\\chromedriver.exe")
driver.get('https://www.bluenile.com/diamond-search')
You can access that page with undetected_chromedriver
import undetected_chromedriver as uc
browser = uc.Chrome()
url = 'https://www.bluenile.com/diamond-search'
browser.get(url)
You can install the package with pip install undetected-chromedriver. Make sure your chrome browser is up to date, and your chromedriver is compatible with it.

Selenium starts the browser but shows an error as Message: Can not connect to the Service

The code starts the browser, stops at this step (line 5), and after a while throws an error:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files\Mozilla Firefox\firefox.exe
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
s = Service(r'C:\Program Files\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(service=s)
driver.get('http://www.google.com')
myPageTitle = driver.title
print(myPageTitle)
driver.quit()
Firefox - 95.0.2
Selenium - 4.1.0
I tried with chrome, same problem
Does anyone know what the problem is and how to solve it?
As an argument to Service() instead of the firefox executable, you need to pass the absolute location of the GeckoDriver executable which can be downloaded from mozilla/geckodriver page.
So your effective code block will be:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
s = Service(r'C:\path\to\geckodriver.exe')
driver = webdriver.Firefox(service=s)
driver.get('http://www.google.com')
myPageTitle = driver.title
print(myPageTitle)
driver.quit()

How to open Chrome browser console through Selenium?

I want to open chrome browser console by pressing keyboard keys Ctrl+Shift+j in selenium webdriver.
I am able to do this action using Robot class but I want this without Robot class. I have used the Actions class and Keys class using sendKeys. But I am unable to open browser console.
Is it chrome browser version issue or OS? Why the browser console is not opening using Action class and Keys class. ?
To open chrome browser console you can use the ChromeOptions class with --auto-open-devtools-for-tabs argument as follows:
Test Configuration:
Selenium: Selenium Standalone Server v3.14.0
ChromeDriver: ChromeDriver 2.46.628402
Chrome: Google Chrome 72.0.3626.96
Code Block:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class A_Chrome_Browser_Console {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
options.addArguments("--auto-open-devtools-for-tabs");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
}
}
Console Output:
Google
Browser Console Snapshot:
You can find a relevant python based discussion in Opening inspect (pressing F12) on Chrome via Selenium

Is it possible to get session_id of active driver opened with Selenium and Appium if several drivers were activated?

While automating I open several browsers, say Firefox, with
driver1 = webdriver.Firefox()
driver2 = webdriver.Firefox()
driver3 = webdriver.Firefox()
.....
Is there a way to get the session_id and webdriver itself of the active Browser?
The same question for Appium. Is it possible to get session_id and driver itself of the active device (virtual or real)?
To get the driver session id with Selenium / Java:
WebDriver driver = new FirefoxDriver();
SessionId session = ((FirefoxDriver)driver).getSessionId();
System.out.println("Session id: " + session.toString());
To get the remote driver session id with Selenium / Java:
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4722/wd/hub"), capabilities);
SessionId session = ((RemoteWebDriver)driver).getSessionId();
System.out.println("Session id: " + session.toString());
((ChromeDriver)driver).sessionId();
There is a workaround for the problem. You could create a Session. This gives you the Webdriver Instance, but also the sessionID.
DefaultDriverFactory defaultDriverFactory = new DefaultDriverFactory(Platform.WINDOWS);
TemporaryFilesystem temporaryFilesystem = TemporaryFilesystem.getDefaultTmpFS();
ChromeOptions chromeOptions = new ChromeOptions();
Session session = DefaultSession.createSession(defaultDriverFactory, temporaryFilesystem, chromeOptions);
WebDriver webDriver = session.getDriver();
SessionId sessionId = session.getSessionId();
Use DriverFactory. The following snippet (written in Katalon Studio, but using selenium, so I guess it would be similar or same in other tools)
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import org.openqa.selenium.WebDriver as WebDriver
WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new ChromeDriver()
DriverFactory.changeWebDriver(driver1)
driver1.get("https://www.example.com")
println DriverFactory.webDriver
DriverFactory.changeWebDriver(driver2)
driver2.get("https://news.example.com")
println DriverFactory.webDriver
will print out to console:
ChromeDriver: chrome on XP (fc70e83ced12b3e9beed990e88670d8e)
ChromeDriver: chrome on XP (a810d0cf94dbaf1cbd018542f9c983c3)
with session ID in brackets.