Test desktop windows using Selenium WebDriver init on root - python

I know that Selenium WebDriver might be mostly for testing web apps but I've seen that there is possibility to handle also desktop windows. I have this piece of code:
self._host = "http://localhost:4723"
options = {
"app": "Root",
'deviceName': 'WindowsPC',
'platformName': 'Windows',
}
self.root = webdriver.Remote(self._host, options)
self.root.find_element_by_accessibility_id("Word")
And I want to grab here the window with Word document and then edit it. I am getting error:
Traceback (most recent call last):
File "Test.py", line 77, in test_python_is_python
self.root.find_element_by_accessibility_id("Word")
AttributeError: 'WebDriver' object has no attribute 'find_element_by_accessibility_id'
Any ideas? Because for me it looks exactly like in example.
Thanks!

Related

How to solve NameError:Options is not defined in AWS Lambda?

How to solve NameError:Options is not defined in AWS Lambda ?
my attempts
Added selenium module file in awslambda
aws docuemnt refer. but not solve
stackoverflow errormessage search but not solve.
Then, it worked, but the error occurred (cloudwatch).
2021-06-28T12:30:31.892+09:00 START RequestId: 7bb8408e-2b12-4e16-80be-e6f1b0166a60 Version: $LATEST
2021-06-28T12:30:31.892+09:00 Error in Imports
2021-06-28T12:30:31.895+09:00 [ERROR] NameError: name 'Options' is not defined Traceback (most recent call last): File "/var/task/lambda_function.py", line 203, in lambda_handler instance_ = WebDriver() File "/var/task/lambda_function.py", line 25, in __init__ self.options = Options()
selenium sample code
import json
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
def __init__(self):
self.options = Options()
self.options.binary_location = '/opt/headless-chromium'
self.options.add_argument('--headless')
self.options.add_argument('--no-sandbox')
self.options.add_argument('--start-maximized')
self.options.add_argument('--start-fullscreen')
self.options.add_argument('--single-process')
self.options.add_argument('--disable-dev-shm-usage')
def get(self):
driver = Chrome('/opt/chromedriver', options=self.options)
return driver
my goals
Through aws_lambda, I want to collect data through crawling all day long.
Just had a similar issue so perhaps it can help you:
Check if the selenium, chromdriver $ headless-chromium are zipped properly.
so when you upload it as a layer to aws lambda, it has the right pathname for you to import selenium in your lambda code.
you can download the file chrome_headless.zip from: https://github.com/soumilshah1995/Selenium-on-AWS-Lambda-Python3.7
and then zip it again inside that folder, on your local machine:
zip -r chrome_headless.zip chromedriver headless-chromium python
then try to upload it to aws lambda layers, attach it to your function and test it.
all credit goes to Soumil Nitin Shah with his great tutorial:
https://www.youtube.com/watch?v=jWqbYiHudt8

Selenium AttributeError: 'str' object has no attribute 'native_events_enabled'

I input selenium firefox like this in my code,
I am running python 2.7 on windows and using bash with conda
driver = webdriver.Firefox('./firefoxdriver')
This is what my terminal says:
C:\Users\Vinko\Desktop\predictBO-master>bash oakniiv#vinko:/mnt/c/Users/Vinko/Desktop/predictBO-master$ source activate predictboba (predictboba) oakniiv#vinko:/mnt/c/Users/Vinko/Desktop/predictBO-master$ python demotrade_tf.py Traceback (most recent call last): File "demotrade_tf.py", line 99, in <module>
driver = webdriver.Firefox('./firefoxdriver') File "/home/oakniiv/anaconda2/envs/predictboba/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 53, in __init__
self.NATIVE_EVENTS_ALLOWED and self.profile.native_events_enabled) AttributeError: 'str' object has no attribute 'native_events_enabled'
I do not know what else to give you, it looks like the problem is in firefox itself?
It works when I do driver = webdriver.Firefox()
You passed a str as a FirefoxProfile, which is the problem:
driver = webdriver.Firefox('./firefoxdriver')
You want:
profile = webdriver.FirefoxProfile('./firefoxdriver')
driver = webdriver.Firefox(profile)
If you did not intend to use a custom profile, then you can use defaults:
driver = webdriver.Firefox()

uTorrent Automation using pywinauto

I am trying out an utorrent automation using pywinauto lib. I want to add a torrent with URL. This option is under the file menu. I can get as far as opening uTorrent and then nothing happens. I used Swapy for generating this code. The box below opens only when I run the code in swapy. But when I save it into a file and run with cmd, only utorrent opens and a traceback occurs in the cmd.
from pywinauto.application import Application
app = Application().Start(cmd_line=u'"C:\\Users\\User\\AppData\\Roaming\\uTorrent\\u Torrent.exe" ')
torrentdfb = app[u'\xb5Torrent4823DF041B09']
torrentdfb.Wait('ready')
menu_item = torrentdfb.MenuItem(u'&File->Add Torrent from &URL...\tCtrl+U')
menu_item.Click()
app.Kill_()
Traceback:
Traceback (most recent call last):
File "AddTorrent.py", line 5, in <module>
torrentdfb.Wait('ready')
File "C:\Python27\lib\site-packages\pywinauto\application.py", line 380, in Wait
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python27\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out
I am new to python coding and I am not an expert. It would be helpful if you provide the explanation to solve my problem or the code. Thanks!!
uTorrent is spawning another process, this is how I got it:
>>> app.windows_()
[]
>>> app.process
6096
>>> app.connect(title_re=u'^μTorrent.*(build \d+).*')
<pywinauto.application.Application object at 0x000000000405C240>
>>> app.process
4044L
This is a final code working for me (with 32-bit uTorrent and 32-bit Python 2.7):
import pywinauto
app = pywinauto.Application().start(r'uTorrent.exe')
time.sleep(5) # because method connect() has no timeout param yet (planned for 0.6.0)
app.connect(title_re=u'^\u03bcTorrent.*(build \d+).*')
main_window = app.window_(title_re=u'^\u03bcTorrent.*(build \d+).*')
main_window.MenuSelect(u'&File->Add Torrent from &URL...\tCtrl+U')
app.AddTorrentFromURL.Edit.SetText('some URL')
app.AddTorrentFromURL.OK.Click()
Bitness is important. 32-bit uTorrent crashes if I use 64-bit Python.

Adding new portgroups to vmware virtual switches using pysphere

I'm trying to automate the addition of new portgroups to ESXi hosts using pysphere. I'm using the following code fragment:
from pysphere import MORTypes
from pysphere import VIServer, VIProperty
from pysphere.resources import VimService_services as VI
s = VIServer()
s.connect(vcenter, user, password)
host_system = s.get_hosts().keys()[17]
prop = VIProperty(s, host_system)
propname = prop.configManager._obj.get_element_networkSystem()
vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0]
network_system = VIMor(propname, MORTypes.HostServiceSystem)
def add_port_group(name, vlan_id, vswitch, network_system):
request = VI.AddPortGroupRequestMsg()
_this = request.new__this(network_system)
_this.set_attribute_type(network_system.get_attribute_type())
request.set_element__this(_this)
portgrp = request.new_portgrp()
portgrp.set_element_name(name)
portgrp.set_element_vlanId(vlan_id)
portgrp.set_element_vswitchName(vswitch)
portgrp.set_element_policy(portgrp.new_policy())
request.set_element_portgrp(portgrp)
s._proxy.AddPortGroup(request)
However, when I attempt to run it, I get the following error:
>>> add_port_group(name, vlan_id, vswitch, network_system)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 12, in add_port_group
File "/usr/lib/python2.6/site-packages/pysphere-0.1.8- py2.6.egg/pysphere/resources/VimService_services.py", line 4344, in AddPortGroup
response = self.binding.Receive(AddPortGroupResponseMsg.typecode)
File "/usr/lib/python2.6/site-packages/pysphere-0.1.8- py2.6.egg/pysphere/ZSI/client.py", line 545, in Receive
return _Binding.Receive(self, replytype, **kw)
File "/usr/lib/python2.6/site-packages/pysphere-0.1.8- py2.6.egg/pysphere/ZSI/client.py", line 464, in Receive
raise FaultException(msg)
pysphere.ZSI.FaultException: The object has already been deleted or has not been completely created
I've attempted to swap in different values for "vswitch" and "network_system", but I haven't had any success. Has anyone attempted to do something similar with pysphere successfully?
I can accomplish what I need through Powershell, which demonstrates that it isn't a vmware issue, but I don't want to use Powershell in this particular case.
I tried your code on one of our vSpheres.
It seems you are passing the object to set_element_vswitchName rather than the name. Maybe this will help:
vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0].name

Cannot figure out python selenium webdriver move_to_element functionality

I looked at number of examples as well as source code for ActionChains and seemingly am using the code suggested in other examples for hover functionality, but still I cannot get over this exception. The code is as follows:
menu = browser.find_element_by_xpath("//nav/ul/li/a[#href='#'][.='Profile']")
hover = ActionChains(webdriver).move_to_element(menu)
hover.perform()
and the exception is:
Traceback (most recent call last):
File "./test.py", line 56, in <module>
hov.perform()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/action_chains.py", line 44, in perform
action()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/action_chains.py", line 201, in <lambda>
self._driver.execute(Command.MOVE_TO, {'element': to_element.id}))
AttributeError: 'module' object has no attribute 'execute'
At first, I thought it was not working because there is no id attribute on the element, however I confirmed that is not the case (find_element_by_xpath does return correct element and there is some sort of {unique id} assigned to it). My Python skills are quite elementary, but I need to adapt the testing script I working on. I am sure I just don't understand this error.
Thanks for your help!
The first argument to ActionChains is the driver instance you use to control the browser, ie browser in this case. Try the following:
menu = browser.find_element_by_xpath("//nav/ul/li/a[#href='#'][.='Profile']")
hover = ActionChains(browser).move_to_element(menu)
hover.perform()

Categories

Resources