Python selenium script connecting to Microsoft edge without debug option? - python-3.x

For those who might not know selenium is a library in many languages to work with browsers. In this specific case, I'm trying to connect in python script on the browser that the user lunches and can save all the running tabs. There are many things that I'm facing so I separated them into 3 problems. I hope they will be understandable. The main goal is to be able to connect to the browser without debug options.
Example
This is a script that should be able to connect to the browser that was launched with debug options in the command prompt. As I provide later I'm no longer able to connect to the remote browser at 127.0.0.1:9222." I've tried searching for a solution, but I haven't found one that works.
Launching browser in with debug port
"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222
Python script
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
driver = webdriver.Edge(options=edge_options)
driver.get(youtube.com)
printf(driver.title)
1. problem
For some reason, my code can no longer connect to the browser launched by the command mentioned. This is an error message Im facing.
part of the error message
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222
**from chrome not reachable
**
full error message
Traceback (most recent call last):
File "C:\Users\user\OneDrive\Plocha\záložky\msedge - running\Try 18.py", line 7, in <module>
driver = webdriver.Edge(options=edge_options)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\edge\webdriver.py", line 73, in __init__
super().__init__(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__
super().__init__(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222
from chrome not reachable
Stacktrace:
Backtrace:
Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72EE616C2+15186]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EDFA212+827554]
(No symbol) [0x00007FF72EABEBF3]
(No symbol) [0x00007FF72EAAEC8A]
(No symbol) [0x00007FF72EAF013D]
(No symbol) [0x00007FF72EAE5D4A]
(No symbol) [0x00007FF72EB29C56]
(No symbol) [0x00007FF72EB21D23]
(No symbol) [0x00007FF72EAF3B80]
(No symbol) [0x00007FF72EAF2B0E]
(No symbol) [0x00007FF72EAF4344]
Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72ECDC3B0+182752]
(No symbol) [0x00007FF72EBB0095]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72ED3A6EA+42362]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72ED3D425+53941]
Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF72F058AB3+1456595]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE0276A+861690]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE07854+882404]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE079AC+882748]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE1097E+919566]
BaseThreadInitThunk [0x00007FF8D3C726BD+29]
RtlUserThreadStart [0x00007FF8D5A6DFB8+40]
What have I tried? There are many tutorials on using python and chrome, but not many are for an edge. Here are some samples of things I've tried:
https://www.youtube.com/watch?v=Zrx8FSEo9lk
https://www.youtube.com/watch?v=FGaU0OgtbNk&t=181s
https://www.youtube.com/watch?v=pZdMsLKAjs4&t=271s
https://bugs.chromium.org/p/chromedriver/issues/detail?id=710#c3
https://www.geeksforgeeks.org/automated-browser-testing-with-edge-and-selenium-in-python/
https://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session
2. problem
My goal is to be able to connect to the browser that didn't start with debug port. Is there a way around the debug options?
What have I tried? Switching the localhost address with the IP address of processes named "msedge.exe". But it got the same error as mentioned.
3. problem
I'm wondering if there's a way to switch between these windows without having to repeatedly reconnect to each one. Is there a method or tool available that would allow me to easily navigate between these separate windows within the application, or do I have no choice but to reconnect each time? Any advice or suggestions would be greatly appreciated. Multiple windows of the browser
Please be patient, thanks for your help.

Related

Executing script with Selenium inside <iframe> doesn't work, but when doing in Console it does

I'm automating a service with Selenium, and the website I need to access uses iframes tags, and I need to alter the value of some elements inside this tag.
I was using the Edge WebDriver, but I changed to Chrome because I thought it'd solve the problem, and unfortunately, it didn't, so I don't think it had anything to do with the Driver, but I'd rather keep with Chrome, if possible.
This is the line that simulates the script:
gc.execute_script("""document.evaluate('//*[#id="QRYSELECT_WRK_QRYSEARCHTEXT254"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.setAttribute('value','foo')""")
And it raises me an exception about null pointers, but basically, it's saying that the browser couldn't find this element, and, therefore, couldn't change its value.
Ironically, when I open the console and write the following command (which is the same script above):
document.evaluate('//*[#id="QRYSELECT_WRK_QRYSEARCHTEXT254"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.setAttribute('value','foo')
It works.
Here's the traceback to the error when I call execute_script from gc:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
gc.execute_script("""document.evaluate('//*[#id="QRYSELECT_WRK_QRYSEARCHTEXT254"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.setAttribute('value','CEL_ADMITIDOS')""")
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\support\event_firing_webdriver.py", line 88, in execute_script
return self._dispatch("execute_script", (script, self._driver), "execute_script", unwrapped_args)
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\support\event_firing_webdriver.py", line 160, in _dispatch
raise e
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\support\event_firing_webdriver.py", line 157, in _dispatch
result = getattr(self._driver, d_call)(*d_args)
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'setAttribute' of null
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=71.0.3578.137 (86ee722808adfe9e3c92e6e8ea746ade08423c7e),platform=Windows NT 10.0.17134 x86_64)
So I guess it's probably something related to permissions...? But I don't know, it's quite weird to get two different results to the same script, isn't it?
Thanks!

selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'

I am trying to automate my hybrid app, where I need to enter details in an input field but using send_keys("Text value") is not working in my case. I am getting the exception
selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'
def test_login(self):
self.driver.implicitly_wait(15)
loginemail = self.driver.find_element_by_id("userId")
loginpass = self.driver.find_element_by_id("userPassword")
email = loginemail.find_element_by_xpath("//*[#id='userId']/input")
email.click()
email.send_keys("xxxxlabs.com")
Here is the full error message:
File "/home/martial/PycharmProjects/pytestAndroid/test_login_android.py", line 45, in test_login
email.send_keys("xxxxlabs.com")
File "/home/martial/PycharmProjects/pytestAndroid/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "/home/martial/PycharmProjects/pytestAndroid/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 491, in _execute
return self._parent.execute(command, params)
File "/home/martial/PycharmProjects/pytestAndroid/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "/home/martial/PycharmProjects/pytestAndroid/venv/lib/python3.6/site-packages/appium/webdriver/errorhandler.py", line 29, in check_response
raise wde
File "/home/martial/PycharmProjects/pytestAndroid/venv/lib/python3.6/site-packages/appium/webdriver/errorhandler.py", line 24, in check_response
super(MobileErrorHandler, self).check_response(response)
File "/home/martial/PycharmProjects/pytestAndroid/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'
I was getting this error when using chromedriver v2.33 - when I updated to v2.36 the error went away (this was on the mac using the chromedriver_mac64.zip file downloaded from: https://sites.google.com/a/chromium.org/chromedriver/downloads)
Question: org.openqa.selenium.webdriverexception: unknown error: call function result missing 'value'. Please look into below steps to resolve such error.
Answer: Update browser version. Please follow below steps to get the latest version of browsers.
Go to: https://www.seleniumhq.org/download/
CTRL+F and search "Third Party Browser Drivers". All browsers are listed there.
Click on browser name/version -It redirects to the another page. (E.g.- http://chromedriver.storage.googleapis.com/index.html?path=2.36/)
Download appropriate driver for your operating system.
Place driver into one of folder from where you are calling that driver to your selenium webdriver scripts. It works without any error.
Thanks :) !!

Opening Firefox via Selenium in Jenkins

When trying to run a Python Selenium script via Jenkins, it crashes, with a very unhelpful exception - "selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 0"
The same Python script runs perfectly fine from outside Python.
I've reduced the script to a very, very basic one, simply launching Firefox -
from selenium import webdriver
if __name__ == "__main__":
print("Hello World")
driver = webdriver.Firefox()
driver.get("http://www.google.com")
driver.maximize_window()
driver.quit()
print("Goodbye World")
Output from cmd prompt:
>C:\Users\kipod>C:\python_projects\Ranger\_just_open_browser.py
>Hello World
>Goodbye World
Works fine.
Running as a Jenkins job:
>##Started by user anonymous
>Building in workspace C:\Program Files (x86)\Jenkins\workspace\Second Test
>[Second Test] $ cmd /c call >C:\Users\kipod\AppData\Local\Temp\jenkins3557827225974274191.bat
>
>C:\Program Files (x86)\Jenkins\workspace\Second Test>python >C:\python_projects\Ranger\_just_open_browser.py
>Hello World
>Traceback (most recent call last):
> File "C:\python_projects\Ranger\_just_open_browser.py", line 8, in <module>
> driver = webdriver.Firefox()
> File "C:\Python\Python36-32\lib\site->packages\selenium\webdriver\firefox\webdriver.py", line 154, in __init__
> keep_alive=True)
> File "C:\Python\Python36-32\lib\site->packages\selenium\webdriver\remote\webdriver.py", line 151, in __init__
> self.start_session(desired_capabilities, browser_profile)
> File "C:\Python\Python36-32\lib\site->packages\selenium\webdriver\remote\webdriver.py", line 240, in start_session
> response = self.execute(Command.NEW_SESSION, parameters)
> File "C:\Python\Python36-32\lib\site->packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
> self.error_handler.check_response(response)
> File "C:\Python\Python36-32\lib\site->packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
> **raise exception_class(message, screen, stacktrace)
>selenium.common.exceptions.WebDriverException: Message: Process unexpectedly >closed with status: 0**
>C:\Program Files (x86)\Jenkins\workspace\Second Test>exit 1
>Build step 'Execute Windows batch command' marked build as failure
>Finished: FAILURE
I suspect something is off with Jenkins's environment variables, but I have no idea what to check. And the exception is so unspecific ("I crashed. lols"), That I found nothing helpful on Google. And I've been searching for a while.
Versions:
Jenkins: 2.73.1
Python: 3.6.2
Geckodriver: 0.19.0
Selenium: 3.6.0
(Tried opening chrome instead of firefox - same behaviour.)
I think you need to run your python script using a jenkins slave configured to log on as a standard windows user account (for example "kipod") ? Make sure that auto login is set for the slave machine for the user account, in order to allow selenium to launch Firefox in a desktop.
Some documenation here: Step by step guide to set up master and slave machines on Windows

Selenium: NoSuchFrameException for Chrome Extension

I have a Chrome extension and I'd like to test it with Selenium. The extension places an iframe tag to pages.
I can successfully get the iframe tag by running the following command:
top_frame = driver.find_element(By.XPATH, value='//iframe[#class="top-frame"]')
However, when I try switching to the frame I get 'NoSuchFrameException' error.
driver.switch_to.frame(top_frame)
The following line outputs the frame's class name correctly
print(top_frame.get_attribute('class'))
Error log:
Testing http://www.google.com
Traceback (most recent call last):
File "test/run_tests.py", line 200, in <module>
main()
File "test/run_tests.py", line 197, in main
run_tests(args.plugin, args.out, args.url)
File "test/run_tests.py", line 167, in run_tests
button = check_button(driver, url)
File "test/run_tests.py", line 84, in check_button
driver.switch_to.frame(top_frame)
File "/home/burak/.virtualenv/extension/lib/python3.5/site-packages/selenium/webdriver/remote/switch_to.py", line 87, in frame
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
File "/home/burak/.virtualenv/extension/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/home/burak/.virtualenv/extension/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: no such frame: element is not a frame
(Session info: chrome=60.0.3112.90)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.10.0-30-generic x86_64)
Try this man,
driver.switch_to.frame(driver.find_element(By.XPATH,value=".//iframe[#src='give your frame source name here..']"));
In the latest version of Chrome, most likely there is a bug that breaks the switch_to_frame() call. Therefore, I started using Firefox instead, everything is smooth for now.
This link is to the associated issue report on Chromium. It has not been resolved yet. (25/10/17)
https://bugs.chromium.org/p/chromedriver/issues/detail?id=2019

BlueZ 5 - Connecting to a Bluetooth LE heart rate watch

I'm pretty much new to using BlueZ 5 and I've been experimenting with a Bluetooth LE heart rate watch. I would really appreciate if someone can point me to the right direction.
When I run the "test-heartrate" test, I see the following error:
Traceback (most recent call last):
File "./test-heartrate", line 88, in <module>
properties = dev_prop.GetAll(HEARTRATE_INTERFACE)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line
651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs:
No such interface 'org.bluez.HeartRate1'
Not sure what I'm doing wrong. Here're some relevant information
BlueZ version installed: 5.24
For installation, I followed the steps listed in: http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html (with one addition: I added --enable-experimental during configuration)
It seems I'm facing the same issue as described at:
http://comments.gmane.org/gmane.linux.bluez.kernel/47931
I've followed the suggestion provided as well as tried passing -E to
bluetoothd while running it but to no avail.
I can successfully run:
test-discovery : To discover devices, including the heart-rate watch
test-device connect : To connect to this device
But the one that is failing for me is test-heartrate
Even after connecting to the device, when I try to introspect:
dbus-send --system --dest=org.bluez --print-reply [obj_path] org.freedesktop.DBus.Introspectable.Introspect, I don't see the
org.bluez.HeartRate1 interface showing up, with its appropriate methods.
I can post the entire introspection data, along with the device properties that I get, if that helps.
Would really appreciate any helpful pointer in this regard. Thanks!!

Resources