Selenium won't execute script on page - python

I'm using Selenium (with python) to monitor a page periodically and save screenshots. This is done on a raspberry pi (armv6). Because of this, I'm using PhantomJS.
I'm monitoring certain elements, and since a raspberry pi isn't the fastest, I am executing a script with driver.execute_script to make the page smaller so that the screenshots aren't too big.
Unfortunately, when executing this script, the following error is thrown:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "`**path**`/bot.py", line 39, in __init__
self.graphs(self.threads['graphs'])
File "`**path**`/bot.py", line 93, in graphs
self.driver.execute_script(`**script**`)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 543, in execute_script
'args': converted_args})['value']
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: {"errorMessage":"Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: \"script-src 'self' *.google-analytics.com\".\n","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"716","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:46451","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"script\": `**script**`, \"args\": [], \"sessionId\": \"69182660-cd69-11e7-b52d-9131a53fa749\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/69182660-cd69-11e7-b52d-9131a53fa749/execute"}}
Screenshot: available via screen
I've googled this error but I haven't been able to find a solution. Is this Selenium that's preventing me from executing this script or is a PhantomJS?
Thanks!
[edit]
Here's the code that's initializing the selenium webdriver:
def __init__(self):
self.driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any', '--web-security=false'])
self.driver.set_window_size(1070, 800)
self.threads = {'check_login': threading.Event(), 'graphs': threading.Event()}
self.login()
self.graphs(self.threads['graphs'])
self.check_login(self.threads['check_login'])

Related

Internet Explorer NOT working in headless mode with Selenium [Using headless_ie_selenium]

I am aware that IE does not support headless. But there's a workaround of using virtual desktops on Windows to get it done. This is what https://github.com/kybu/headless-selenium-for-win is doing, but I seem to be running into an issue here.
My IEDriverServer.exe is added to $PATH$, and headless_ie_selenium.exe is also in the same directory as the IEDriverServer.exe
I'm trying to use it like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Ie('C:\IEDriverServer_Win32_3.150.1\headless_ie_selenium.exe')
browser.get('www.someurl.com')
I'm pasting the traceback below
Traceback (most recent call last):
File "C:\Users\hjind\Desktop\pythonRCM\headless_ie.py", line 16, in <module>
browser = webdriver.Ie('C:\IEDriverServer_Win32_3.150.1\headless_ie_selenium.exe')
File "C:\Users\hjind\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium-3.141.0-py3.9.egg\selenium\webdriver\ie\webdriver.py", line 93, in __init__
RemoteWebDriver.__init__(
File "C:\Users\hjind\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium-3.141.0-py3.9.egg\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\hjind\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium-3.141.0-py3.9.egg\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\hjind\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium-3.141.0-py3.9.egg\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\hjind\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium-3.141.0-py3.9.egg\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unexpected error launching Internet Explorer. IELaunchURL() returned HRESULT 80070012 ('There are no more files.') for URL 'http://localhost:57762/'
The only thing that worked for me was to programatically minimise the IE window on launch, and switch to another virtual desktop while the job runs.
Setting introduce_flakiness_by_ignoring_security_domains helped bypass the "Protected Mode" and "Zoom Level" errors.

Python, Selenium Chrome driver, Failing to Interact with Alerts

I am trying to build a bot and that needs to be able to interact with permission boxes. This bot is supposed to click accept/dismiss depending on what the user input; however, every time I attempt to interact with the permissions popup that appears in the html file I get the error mentioned below. I have lightly looked into the Alert object, and I have tried driver.switch_to_alert(), but, I find that that has been deprecated. I running the latest version of python, and the the latest version of selenium, along with the latest version of the chrome driver. The error message below, is the same message I get for all methods I have attempted to use to solve this problem.
driver.switch_to_alert()
Traceback (most recent call last):
File "C:/Users/micha/PycharmProjects/botty/alert_testing.py", line 9, in <module>
driver.switch_to_alert()
File "C:\Users\micha\.virtualenvs\botty-KuDX6gMP\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 803, in switch_to_alert
return self._switch_to.alert
File "C:\Users\micha\.virtualenvs\botty-KuDX6gMP\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 55, in alert
alert.text
File "C:\Users\micha\.virtualenvs\botty-KuDX6gMP\lib\site-packages\selenium\webdriver\common\alert.py", line 67, in text
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
File "C:\Users\micha\.virtualenvs\botty-KuDX6gMP\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\micha\.virtualenvs\botty-KuDX6gMP\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
(Session info: chrome=86.0.4240.111)
Process finished with exit code 1
My code:
from selenium.webdriver import Chrome
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.alert import Command
from time import sleep
driver = Chrome()
driver.get('file:///C:/Users/micha/Scripts/web%20design/javaScriptTesting/jscriptTesting.html')
sleep(3)
al = driver.switch_to.alert
al.accept()
input()

Selenium Webdriver.get() throws a timeout error even though page has loaded

I am using selenium to automate some work that I do in Google Analytics. The program was working fine for months until a few days back.
It first logs into google and then opens the relevant analytics pages and gets the required information.
The logging in process works fine and it opens the first analytics page and saves all the info.
Then when I make it move to another page by using the get function, the page loads in a few seconds but the program gets stuck and throws a timeout error after a few mins.
This is the part of the code that gets stuck:
## Getting GEOLOCATIONS
print("Getting GeoLocations")
driver.get("{}{}/{}".format(geourl, key[i], geo[i]))
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((
By.XPATH,"//*[#id=\"ID-reportHeader-reportToolbar"]/div[1]/div[2]/span[2]"
)))
The code gets stuck in the get() function. This is the error after a few mins:
Getting GeoLocations
Traceback (most recent call last):
File "seo.py", line 122, in <module>
driver.get("{}{}/{}".format(geourl, key[i], geo[i]))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 248, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
(Session info: chrome=64.0.3282.140)
(Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.4.0-112-generic x86_64)
I am using the latest chromedriver 2.35 and selenium version 3.90.

Python Selenium decipher error message

I am trying to track down an error in Python Selenium. The error message is..
Traceback (most recent call last):
File "C:\myscipt\main.py", line 110, in <module>
source_mysource(func1, func2, func3, func4, func5, func6, func7, func8, func9)
File "C:\myscipt\sources\functions.py", line 132, in source_mysource
current_url = driver.current_url
File "C:\Users\tom\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 454, in current_url
return self.execute(Command.GET_CURRENT_URL)['value']
File "C:\Users\tom\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\tom\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.
<super: <class 'WebDriverException'>, <UnexpectedAlertPresentException object>>
Is the error saying that the line current_url = driver.current_url is the one that is triggering the error or is it the next line?
When you use .current_url property of a WebDriver instance, a GET_CURRENT_URL webdriver command is sent, which is triggering an error in your case. What happens is, the currently opened web-page is not the current top-level browsing context because of the alert being present, and, according to the specification, it should and fails with an error.
In other words, current URL cannot be retrieved when there is an opened active alert.

How do I click Enter using Splinter?

Here is my Splinter code -
b = Browser()
b.visit("http://boingboing.net")
b.fill("q", "OpenXC")
At this point, I would like to press "Enter" for the search to happen.
This is very similar to the example in the Splinter tutorial page. In this instance, there is really no button element present.
I see that this Search box is a Google Search Box with an id "cse-search-box". I do not know how to trigger search here.
Can you please help?
Metaphy's (thanks, btw) code below generates a traceback for me -
Traceback (most recent call last):
File "/Users/muthu/Desktop/boing.py", line 5, in
b.execute_script('document.getElementsByName("f")[0].submit()')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/init.py", line 58, in execute_script
self.driver.execute_script(script)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 397, in execute_script
{'script': script, 'args':converted_args})['value']
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'waiting for evaluate.js load failed' ; Stacktrace:
at r (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:8360)
at fxdriver.Timer.prototype.runWhenTrue/g (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:392)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:386)
I can not access the http://boingboing.net site, so I take baidu.com for example:
from splinter.browser import Browser
b = Browser('firefox')
b.visit('http://www.baidu.com')
b.fill('wd', 'test')
b.execute_script('document.getElementsByName("f")[0].submit()')
New answer:
browser.fill('some_name', 'some_value\r')

Categories