Selenium google crashes when running tests - python

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Hello world' + Keys.ENTER)`
Help me pls,who faced such problem? There is a solution?
The browser starts and after a couple of seconds it closes, although I did not give the command to close. What's wrong?

Try using the detach option when initializing the chromedriver. As following:
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("start-maximized")
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(options=chrome_options, service=s)

Related

Selecting value from dropdown-box is not possible with selenium?

I try to select the value "Ukrainian Division" in the dropdown box of the following site:
https://www.cyberarena.live/schedule-efootball
with the following code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
import time
if __name__ == '__main__':
WAIT = 3
options = Options()
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
link = f"https://www.cyberarena.live/schedule-efootball"
driver.get (link)
time.sleep(WAIT)
select = Select(driver.find_elements(By.XPATH,"//select")[1])
select.select_by_visible_text('Ukrainian Division')
# select.select_by_value("1")
input("Press!")
driver.quit()
But unfortunately, nothing happens - the options are not selected with this code.
I also tried it with select_by_value with this line
select.select_by_value("1")
instead of
select.select_by_visible_text('Ukrainian Division')
but this doesn´t work either.
How can I select this option from the dropdown box?
I tried ypur code and I also could not use Selenium Select object there. I don't know why. But we still can do that directly, with regular Selenium commands.
The following code is working:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.cyberarena.live/schedule-efootball'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//select[contains(.,'Division')]"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Ukrainian')]"))).click()
The result is:

TypeError: EnumMeta.__call__() missing 1 required positional argument: 'value'

I'm looking to setup a webdriver in a script as a headless. I'm able to run it as a non headless way but when i'm creating an instance of the Option() it says me missing 1 required positional argument: 'value'
chrome_options = Options()
Here's a replication of the issue I'm having on the project.
from selenium import webdriver
from webbrowser import Chrome
from ssl import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class PythonOrg():
def Setup(self):
self.chrome_options = Options()
self.chrome_options.add_argument("--headless")
# self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) #not a headless
self.driver = webdriver.Chrome(options=chrome_options)
def GetLink(self):
driver = self.driver
driver.get('https://www.python.org')
print(driver.title)
driver.close()
inst = PythonOrg()
inst.Setup()
inst.GetLink()
Note: I'm new to Python!
You imported a wrong import.
Instead of
from ssl import Options
It should be
from selenium.webdriver.chrome.options import Options
for chrome_options add self.
and use from selenium.webdriver.chrome.options import Options not from ssl import Options
from selenium import webdriver
from webbrowser import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class PythonOrg():
def Setup(self):
self.chrome_options = Options()
self.chrome_options.add_argument("--headless")
# self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) #not a headless
self.driver = webdriver.Chrome(options=self.chrome_options)
def GetLink(self):
driver = self.driver
driver.get('https://www.python.org')
print(driver.title)
driver.close()
inst = PythonOrg()
inst.Setup()
inst.GetLink()

Accept/bypass cookies with Selenium

I'm trying to get a screen shot from of a website from an url
however how can I accept the cookies before the screen shot ?
If you have any idea or clue I will appreciate.
Here is a following picture example of cookies :
Here is my code:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
start = time.time()
browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get("https://candidat.pole-emploi.fr/offres/recherche/detail/136LNXS/")
browser.save_screenshot('screenshot1.png')
browser.quit()
end = time.time()
print("tempo =","\t",end - start)
You have to click on accept-button to accept cookie
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
#from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
#chrome to stay open
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
driver.get('https://candidat.pole-emploi.fr/offres/recherche/detail/136LNXS')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="footer_tc_privacy_button_2"]'))).click()

Selenium 4 and auto-closing browser

I have script on selenium 3 and it works fine:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://ya.ru/')
driver.find_element_by_name('text').send_keys('some text')
driver.find_element_by_class_name('search2__button').click()
Now i reworked it for selenium 4, but now browser closing on its own when code ends:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://ya.ru/')
driver.find_element(By.NAME, 'text').send_keys('some text')
driver.find_element(By.CLASS_NAME, 'search2__button').click()
I want to keep browser open.
I found this answer here.
You need to set the "detach" option to True when starting chromedriver:
from selenium.webdriver import ChromeOptions, Chrome
opts = ChromeOptions()
opts.add_experimental_option("detach", True)
driver = Chrome(chrome_options=opts)
One simple way would be to add a dumpy input() at the end of the code.
on_hold = input("Enter anything on the console to exit.")
Can you try this code out, it didn't auto close for me. Don't forget to change the path to the location of the chromedriver.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
ser = Service("path/to/chromedriver.exe")
op = webdriver.ChromeOptions()
driver=webdriver.Chrome(service=ser,options=op)
driver.maximize_window()
driver.get('https://ya.ru/')
driver.find_element(By.NAME, 'text').send_keys('some text')
driver.find_element(By.CLASS_NAME, 'search2__button').click()

Selenium closes browser right after opening

I am trying to open Brave using Selenium (in Python). It actually opens but then immediately closes with the following errors appearing in the console:
[23340:9252:1107/063438.209:ERROR:os_crypt_win.cc(93)] Failed to
decrypt: The parameter is incorrect. (0x57)
[23340:9252:1107/063438.210:ERROR:brave_sync_prefs.cc(114)] Decrypt
sync seed failure
DevTools listening on
ws://127.0.0.1:53809/devtools/browser/ecce3b0e-2884-4173-bdab-2215a3d7f507
[23340:9252:1107/063438.480:ERROR:CONSOLE(1)] "[Shields]: Can't
request shields panel data for tabId: 2. Error: No tab url
specified", source:
chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd/out/brave_extension_background.bundle.js
(1)
I've done some searching but couldn't really find anything helpful.
Here is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Selen:
def __init__(self):
options = Options()
service = Service("C:/Auxiliary/chromedriver_win32/chromedriver.exe")
options.binary_location = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
self.driver = webdriver.Chrome(service=service, options=options)
self.driver.get("https://google.com")
Selen()
I'm using Windows 11.
Firstly, do you have chrome installed, and secondly, does it work if you do this?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Selen:
def __init__(self):
options = Options()
path = "C:/Auxiliary/chromedriver_win32/chromedriver.exe"
self.driver = webdriver.Chrome(executable_path=path, options=options)
self.driver.get("https://google.com")
time.sleep(20)
driver.close()
instance = Selen()
If it does work, try this to get it to work with brave,
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Selen:
def __init__(self):
options = Options()
options.binary_location = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
path = "C:/Auxiliary/chromedriver_win32/chromedriver.exe"
self.driver = webdriver.Chrome(executable_path=path, options=options)
self.driver.get("https://google.com")
time.sleep(20)
driver.close()
instance = Selen()

Categories

Resources