I am using selenium to do a bit of automation, but I would like to be able to keep the browser windows open even after the python console has been closed.
Here are my current settings for the webdriver:
capabilities = {
'browserName': 'chrome',
'version': '',
'platform': 'ANY',
'javascriptEnabled': True,
'chromeOptions': {
'useAutomationExtension': False,
'forceDevToolsScreenshot': True,
'detach': False,
'args': ['--start-maximized', '--disable-infobars', '--log-level=3']
}
}
driver = webdriver.Chrome(desired_capabilities=capabilities)
Does anyone know how I can achieve this? Thanks.
Related
I am trying to add cookies to the selenium web driver. I am specifying it like this
driver.add_cookie({'domain': '.facebook.com',
'expiry': 1456567765,
'httpOnly': False,
'name': 'fr',
'path': '/',
'sameSite': 'None',
'secure': True,
'value': "scsvdsvbrsdvasvdsgdssdv"})
Tries also this way
for i in cookies:
drvier.add_cookie(i)
However, I am getting this error.
Message: invalid session id
Stacktrace:
0 chromedriver 0x000000010f17e788 chromedriver + 4515720
https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-canEmulate
enter image description here
If you look at the Emulation.setUserAgentOverride section of the developer protocol site here, there is the ability to enter a userAgentMetadata parameter, but Python Selenium doesn't recognize the parameter.
I want to customize Sec-Ch-Ua.
When I use the return navigator.userAgentData code I want it to come out like this.
{'brands': [{'brand': '.Not/A)Brand', 'version': '99'}, {'brand': 'Google Chrome', 'version': '103'}, {'brand': 'Chromium', 'version': '103'}], 'mobile': False, 'platform': 'Windows'}
I try to set experimental_option on ChromeDriver by Selenoid UI. I tried a lot of cases but neither works, usually, I see a normal browser without mobile emulation in local Selenoid UI or browser is set as unknown when I try another way and I don't have a capabilities dictionary.
My code now, which doesn't work correctly:
capabilities = {
"browserName": "chrome",
"version": "80.0",
"enableVNC": True,
"enableVideo": video,
'screenResolution': "1920x1080",
"chromeOptions": {
"mobileEmulation": {
"deviceName": "Nexus 5"
}
}
}
context.browser = webdriver.Remote(
command_executor="http://selenoid:4444/wd/hub",
desired_capabilities=capabilities)
Maybe somebody knows how to set experimental options in the chrome image in Selenoid UI?
I'm trying to write in some defensive code to prevent someone from executing a script should they have an older version of geckodriver installed. I cannot for the life of me seem to get the geckodriver version from the webdriver object.
The closest I found is driver.capabilities which contains the firefox browser version, but not the geckodriver version.
from selenium import webdriver
driver = webdriver.Firefox()
pprint(driver.capabilities)
output:
{'acceptInsecureCerts': True,
'browserName': 'firefox',
'browserVersion': '60.0',
'moz:accessibilityChecks': False,
'moz:headless': False,
'moz:processID': 18584,
'moz:profile': '/var/folders/qz/0dsxssjd1133p_y44qbdszn00000gp/T/rust_mozprofile.GsKFWZ9kFgMT',
'moz:useNonSpecCompliantPointerOrigin': False,
'moz:webdriverClick': True,
'pageLoadStrategy': 'normal',
'platformName': 'darwin',
'platformVersion': '17.5.0',
'rotatable': False,
'timeouts': {'implicit': 0, 'pageLoad': 300000, 'script': 30000}}
Is it possible the browser version and geckodriver versions are linked directly? if not, how can I check the geckodriver version from within python?
There is no method in the python bindings to get the geckodriver version, you will have to implement it yourself, my first option would be subprocess
# Mind the encoding, it must match your system's
output = subprocess.run(['geckodriver', '-V'], stdout=subprocess.PIPE, encoding='utf-8')
version = output.stdout.splitlines()[0].split()[-1]
It appears that moz:geckodriverVersion has been added to the capabilities sometime late 2018.
driverversion = driver.capabilities['moz:geckodriverVersion']
browserversion = driver.capabilities['browserVersion']
I am using Python Selenium Chrome WebDriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
and
self.driver = webdriver.Chrome(chrome_options=options, desired_capabilities=capabilities)
print self.driver.get('https://192.168.178.20:1337/login?email=me#domain.com&password=mypassword')
print self.driver.get('https://192.168.178.20:1337/this/that?name=john')
Before I didn't need to authenticate and my GET went through, but now I do using PUT request with email and password params. I have tested the PUT in POSTMAN and it worked fine.
Once authenticated, I want to browse to another URL using GET, but I am getting a 500 most likely because it didn't retain that I have authenticated.
How can I check that my login worked? How do I retrieve the response?
Do I need to retrieve & save some kind of token or cookie for the 2nd request go through?
Console log
headless_chrome > Auth
None
headless_chrome > GET
None
headless_chrome > CONSOLE
headless_chrome console > {u'source': u'network', u'message': u'https://192.168.178.20:1337/this/that?name=john 0:0 Failed to load resource: the server responded with a status of 500 (Internal Server Error)', u'timestamp': 1479212713208, u'level': u'SEVERE'}
headless_chrome > title:
{'_file_detector': <selenium.webdriver.remote.file_detector.LocalFileDetector object at 0x7f8507a25f50>,
'_is_remote': False,
'_mobile': <selenium.webdriver.remote.mobile.Mobile object at 0x7f8507a25d90>,
'_switch_to': <selenium.webdriver.remote.switch_to.SwitchTo instance at 0x7f8507a336c8>,
'capabilities': {u'acceptSslCerts': True,
u'applicationCacheEnabled': False,
u'browserConnectionEnabled': False,
u'browserName': u'chrome',
u'chrome': {u'chromedriverVersion': u'2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a)',
u'userDataDir': u'/tmp/.com.google.Chrome.ybR9Fm'},
u'cssSelectorsEnabled': True,
u'databaseEnabled': False,
u'handlesAlerts': True,
u'hasTouchScreen': False,
u'javascriptEnabled': True,
u'locationContextEnabled': True,
u'mobileEmulationEnabled': False,
u'nativeEvents': True,
u'platform': u'Linux',
u'rotatable': False,
u'takesHeapSnapshot': True,
u'takesScreenshot': True,
u'version': u'50.0.2661.102',
u'webStorageEnabled': True},
'command_executor': <selenium.webdriver.chrome.remote_connection.ChromeRemoteConnection object at 0x7f8507a25cd0>,
'error_handler': <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7f8507a25d50>,
'service': <selenium.webdriver.chrome.service.Service object at 0x7f85083bc3d0>,
'session_id': u'72a5ce48d950be26b3f33de4adb34428',
'w3c': False}
headless_chrome > except else
clean up Selenium browser
Selenium webdriver has no .put method. You should use .get for both authentication and then navigating to another url. Ideally this should work.
If that works manually then it should work through webdriver also.