Undetected-Chromedriver Close Chrome Automatically After Few Seconds - python-3.x

I'm using:
Python 3.9
Chrome 93.0.4577.63
ChromeDriver 93.0.4577.15
Undetected-Chromedriver 3.0.3
Windows 10
I use the most simple code to open Chrome:
import undetected_chromedriver.v2 as uc
driver = uc.Chrome ()
with driver:
driver.get ('https://www.google.com/')
When Chrome is opened, in the up-right corner there is a message of Error, when I click on it says, New Extension Added, (Another program on your computer has added an extension that can change how Chrome works)
After a few seconds Chrome is closed automatically
Anybody know that cause it and what it's the solution?
Thank you for your answer.

Is's simple your code just end after get google.com
Add sleep or cycle with key for interupt.
For example:
import undetected_chromedriver.v2 as uc
import os
driver = uc.Chrome ()
with driver:
driver.get ('https://www.google.com/')
os.system('pause')

Related

Selenium Python - Internet Explorer - Problem

I'm new on Selenium and I'm trying to do a task using Internet Explorer (I'm using Python 3.8)
In order to uderstand the commands of Selenium, I tried to run the simple code below
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Ie(executable_path="C:\\Users\\usuario\\Downloads\\IEDriverServer.exe")
driver.get("https://www.google.com.br/")
driver.set_page_load_timeout(10)
driver.find_element_by_name("q").send_keys("Ronaldinho Gaucho")
driver.find_element_by_name("btnK").send_keys(Keys.ENTER)
driver.maximize_window()
driver.refresh()
The page opens, however, nothing is typed on the search bar on Google website, I have seen this code in a Youtube video and it has worked well, but when I try on my computer, it does not works (it does not raise any error on my terminal)
Anyone can help?, what I should been looking for?
Thanks in advance

I am using Microsoft Edge Chromium with Selenium and I keep on getting the msedge.exe as a startup item

This is the code I used for using Selenium with Microsoft Edge Chromium browser:
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.webdriver import WebDriver
driveroptions = Options()
driveroptions.use_chromium = True
driveroptions.add_argument('--start-maximized')
driveroptions.binary_location = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
service = Service(executable_path="msedgedriver.exe")
driver = webdriver.Edge(options=driveroptions, service=service)
And whenever I restart the system, I get more than 100 msedge.exe in the startup under Task Manager and get 100s of popups of msedge.exe command prompt like this Popups picture. I deleted them from the startup with Autoruns but I got them again after restart. Has anyone else encountered this issue?
Had the same issue - disabling Microsoft Edge instances in "Startup Apps" helped but new instances got created every time the program runs...
Solved by using msedge-selenium-tools as recommended by MSDN (https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium?tabs=python)
Issue hasn't cropped up in several days.
Example code:
from msedge.selenium_tools import Edge, EdgeOptions
driverOptions = EdgeOptions()
driverOptions.use_chromium = True
driverOptions.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driverOptions.add_argument("--headless")
driverOptions.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = Edge(options=driverOptions)
#... use your driver
driver.close()
driver.quit()
EDIT:
Did some more testing and it looks like it is just the "driverOptions.add_experimental_option("excludeSwitches", ["enable-logging"])" that fixes this issue. (NOT using msedge.selenium_tools).

how to properly call the edge browser with selenium?

The target of this project is to automate checking sites with Microsoft edge browser using selenium-python i downloaded the webdriver for the edge legacy from this link and i went for the latest release 17134 extracted it with out any problems now lets say i want to visit facebook in an automated way with firefox using the geckodriver
firefox code sample with selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
# setting up headless option for faster execution
options = Options()
options.headless = True
browser = (webdriver.Firefox(options=options))
browser.get('https://www.facebook.com/')
but when I try to use Microsoft edge that is built in windows 10 I get an attribute error
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options
options = Options()
options.headless = True
#browser = webdriver.edge(options=options)
browser = webdriver.edge()
ps : when I uncomment this part (browser = webdriver.edge(options=options)) I get module not found error
what is the right way to call Microsoft edge browser , or what I am doing wrong
when I use Edge and try to make Edge headless. I also find it hard to do that with slight changes as Chrome. And I refer to the official documentation and get a official solution. Besides selenium, you need to install msedge-selenium-tools, just pip install itpip install msedge-selenium-tools. And use Edge Class in msedge tools. Just like:
from msedge.selenium_tools import Edge
driver = Edge(executable_path='where')
And if we want to make Edge headless, we need to use EdgeOptions Class which selenium.webdriver doesn't offer. selenium.webdriver only provides us with ChromeOptions, FirefoxOptions and Ie's. EdgeOptions is in a separated package msedge.selenium_tools.Then we add argument as what we do on Firefox or Chrome. Before that, we need to set the attribute use_chromium as True. The whole codes:
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
# make Edge headless
edge_options = EdgeOptions()
edge_options.use_chromium = True # if we miss this line, we can't make Edge headless
# A little different from Chrome cause we don't need two lines before 'headless' and 'disable-gpu'
edge_options.add_argument('headless')
edge_options.add_argument('disable-gpu')
driver = Edge(executable_path='where', options=edge_options)
Hope it helps. Sorry for my awkward explaination.
I am using this WebDriver Package. Works perfectly. This package auto-download and runs your system-compatible browser smoothly. If you want to install and run a specific version, that is also possible. To know the instructions click here.
This code is for Selenium 4 [Python 3.10.*]
class MyEdge:
def get_browser(self):
options = webdriver.EdgeOptions()
# If you want to avoid popup browser use '--headless'
options.add_argument('--headless')
# Ref: https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=python#using-chromium-specific-options
self.driver = webdriver.Edge(options= options,service=Service(EdgeChromiumDriverManager().install()))
return self.driver
More optional arguments:
--headless, --no-sandbox, '--disable-gpu', '--window-size=1280x1696', '--user-data-dir=/tmp/user-data', '--hide-scrollbars', '--enable-logging', '--log-level=0', , '--single-process', '--data-path=/tmp/data-path', '--ignore-certificate-errors', '--homedir=/tmp', '--disk-cache-dir=/tmp/cache-dir'
Make sure to impore:
# Import
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
I try to refer to the official documentation for WebDriver for Microsoft Edge (EdgeHTML). But I did not get any information about the Headless mode in it.
WebDriver (EdgeHTML)
I also try to refer to some old threads to find any information on this topic. It looks like we cannot use the Headless mode with the MS Edge legacy browser.
Headless Edge driven through Selenium by C#
I found one article that also said that 'User cannot use IE10, IE11, Edge, Opera & Safari for headless testing.'
Headless Browsers Testing using Selenium Webdriver
From the above references, it looks like you cannot use Headless mode with the MS Edge legacy browser.
As a workaround, I suggest you try to make a test with the MS Edge Chromium browser. I found that it supports Headless mode.
Using Chromium-Specific Options

get active url on chrome and firefox using python

I work with python 3.7 on windows and I want to write a script that print the actual active urls on browsers (chrome and firefox), I found the script:
import webbrowser
webbrowser.open(the url)
but this script allows to open the url not to find the active urls.
can someone help me
Here you need to download and install pywin32 and import these modules in your script like this -
import win32gui
import win32con
#to get currently active windows
window = win32gui.GetForegroundWindow
Or to get the Google Chrome window handle
win32gui.FindWindow
You can use selenium module and loop through all open tabs.
This code prints all open tabs of chrome and firefox:
from selenium import webdriver
chromeDriver = webdriver.Chrome()
firefoxDriver = webdriver.Firefox()
for handle in chromeDriver.window_handles[0]:
chromeDriver.switch_to.window(handle)
print(chromeDriver.current_url)
for handle in firefoxDriver.window_handles[0]:
firefoxDriver.switch_to.window(handle)
print(firefoxDriver.current_url)
Note:
This code is inefficient and should change to use only one loop.

Selenium FirefoxOptions - how to set browser window to be maximised at start?

I''m building some Jave/Junit Selenium tests to work with both Chrome and Firefox.
I can't find any info on how to start the Firefox browser window in a maximised state. With Chrome I can use ChromeOptions, like:
anOptions.addArguments("--start-maximized");
I can't find any way to make the same thing happen for Firefox and FirefoxOptions.
Any suggestions?
For python with Firefoxyou can use:
driver = Firefox()
driver.maximize_window()
Once you've started your test and used driver.get('http://your_url') to navigate to your desired URL, you can maximize the browser window by using the following command:
driver.manage().window().maximize();

Resources