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()
Related
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!
This question already has answers here:
ModuleNotFoundError: What does it mean __main__ is not a package?
(6 answers)
Closed 4 years ago.
How to make screenshot from any url (web page)?
I was trying:
from .ghost import Ghost
ghost = Ghost(wait_timeout=4)
ghost.open('http://www.google.com')
ghost.capture_to('screen_shot.png')
Result:
No module named '__main__.ghost'; '__main__' is not a package
I was trying also:
Python Webkit making web-site screenshots using virtual framebuffer
Take screenshot of multiple URLs using selenium (python)
Fastest way to take a screenshot with python on windows
Take a screenshot of open website in python script
I've also tried other methods that are not listed here.
Nothing succeeded. Or an error or module is not found .. or or or.
I'm tired. Is there an easy way to make a screenshot of a web page using Python 3.X?
upd1:
C:\prg\PY\PUMA\tests>py save-web-html.py
Traceback (most recent call last):
File "save-web-html.py", line 2, in <module>
from .ghost import Ghost
ModuleNotFoundError: No module named '__main__.ghost'; '__main__' is not a package
upd2:
C:\prg\PY\PUMA\tests>py save-web-html.py
Exception ignored in: <bound method Ghost.__del__ of <ghost.ghost.Ghost object at 0x0000020A169CF860>>
Traceback (most recent call last):
File "C:\Users\Coar\AppData\Local\Programs\Python\Python36\lib\site-packages\ghost\ghost.py", line 325, in __del__
self.exit()
File "C:\Users\Coar\AppData\Local\Programs\Python\Python36\lib\site-packages\ghost\ghost.py", line 315, in exit
self._app.quit()
AttributeError: 'NoneType' object has no attribute 'quit'
Traceback (most recent call last):
File "save-web-html.py", line 4, in <module>
ghost = Ghost(wait_timeout=4)
TypeError: __init__() got an unexpected keyword argument 'wait_timeout'
In the late 80's this may have been a simple task, just render some html to an image instead of the screen.
But these days web-pages require client-side execution to build parts of its DOM and re-render based on client-side initiated AJAX (or equivalent) requests... it's a whole thing "web 2.0" thing.
Rendering a web-site such as http://google.com as a simple html return should be easy, but rendering something like https://www.facebook.com/ or https://www.kogan.com/ will have many back & fourth comms to display what you're expecting to see.
So restricting this to a pure python solution may not be plausible; I'm not aware of a python-based browser.
Consider running a separate service to take the screenshots, and use your core application (in python) to fetch requested screenshots.
I just tried a few with docker, many of them struggle with https and the aforementioned ajax behaviour.
earlyclaim/docker-manet appears to work demo page
edit: from your comments, you need the data from a graph that's rendered using a 2nd request.
you just need the json return from https://www.minnowbooster.net/limit/chart
try:
from urllib.request import urlopen # py3
except ImportError:
from urllib2 import urlopen # py2
import json
url = 'https://www.minnowbooster.net/limit/chart'
response = urlopen(url)
data_str = response.read().decode()
data = json.loads(data_str)
print(data)
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()
I am running a test script in Python Selenium Firefox and seemingly at random it crashes with the following error...
Time Elapsed: 104.31666666666666
Traceback (most recent call last):
File "D:\sel_scripts\main.py", line 110, in <module>
source_rf_script(driver, time, randint)
File "D:\sel_scripts\data_sources\myscript.py", line 184, in source_rf_script
htmlText = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
File "C:\Users\user4\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute_script
{'script': script, 'args':converted_args})['value']
File "C:\Users\user4\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Users\user4\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: waiting for doc.body failed
Stacktrace:
at injectAndExecuteScript/< (file:///C:/Users/user4/AppData/Local/Temp/tmpvbvr8pjg/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/driver-component.js:
10678)
at fxdriver.Timer.prototype.runWhenTrue/g (file:///C:/Users/user4/AppData/Local/Temp/tmpvbvr8pjg/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/driver
-component.js:629)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/user4/AppData/Local/Temp/tmpvbvr8pjg/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/
driver-component.js:623)
I am trying to work out what is causing it, to me it reads like the driver.execute_script command is causing the failure. Could it be when the page elements fail to load correctly (which happens occasionally on the dev server) and the command is unable to find any?
Am I reading it right?
You can find that error in the JavaScript source.
The logic seems to be that whenever someone calls execute_script(), i.e. on the line:
htmlText = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
... a listener waits up to 10 seconds for a <body> element to (yet) be available in the current document. If one doesn't, you get the error.
I believe the motivation is to alert you that the execution of a very slow script - and trying to grab the innerHtml of the entire doc will be slow - may have blocked the loading of the document that invoked it.
So, two possibilities:
If, as you say, the DOM never loads, then that could misleadingly trigger the error (though it's reasonable to get some kind of error).
Or it could still be a race condition, i.e. DOM loading blocked by misbehaving script.
Solutions would be:
Avoid executing any scripts until you know the DOM exists and is ready.
Find a smarter, quicker way to get the data from the page.
(Obviously you have a right to expect that the page you're testing will be served correctly from the back-end.)
See also this thread: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1157
Despite all attempts, I cannot seem to get Entry().get() to assign a string in an Entry window. Here's a code snippet:
Itx_bcn_ent = Entry(win).grid(row=1,column=1)
I define a button to call a function:
btn = Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)
Here's the function:
def getnums():
Itx_bcn = Itx_bcn_ent.get()
When I run the script, I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Python34\voltage_substate_GUI.py", line 7, in getnums
Itx_bcn = Itx_bcn_ent.get()
AttributeError: 'NoneType' object has no attribute 'get'
I've seen the construct to use a Class StringVar() and the option "textvariable=" with the Entry() object, however doing this seems overly complicated as it just creates an additional set of variables between what's in the Entry window and the variable I am trying to assign.
Any thoughts on this?
Entry.grid() returns None, so when you do something = Entry(root).grid(), you're getting something=None.
This isn't a problem until you try to use that thing! That's why you're getting a 'NoneType' object has no attribute 'get' error.
Itx_bcn_ent = Entry(win)
Itx_bcn_ent.grid(row=1,column=1)
Now your button works :), though you have the same problem with your btn = line. I've yet to have to reuse that assignment, so maybe just drop it?
Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)