Twitch Chat box in selenium - python-2.7

Well I pieced together a script with multiple google searches which is based on selenium,the intention of this code is to try to type in the chat box of twitch with a randomized message automatically(not limited use to only my channel), but it fails miserably. I have a general knowledge on python but that's it, which is why ended up here since I couldn't find the answer myself.
from selenium import webdriver
from random import randint
import keyboard
browser = webdriver.Chrome(executable_path=r'C:\Python27\Scripts\chromedriver')
browser.get('https://www.twitch.tv/alexaki99')
#getting chat box
chat=browser.find_element_by_css_selector('#root > div > div > div.tw-full-height.tw-flex.tw-flex-column.tw-flex-nowrap.tw-relative > div > div.right-column.tw-full-height.tw-flex-shrink-0.tw-relative > div > div > div.tw-full-width.tw-full-height.tw-z-default.tw-flex.tw-flex-column.tw-flex-grow-1.tw-flex-nowrap.tw-relative > div.tw-full-width.tw-full-height.tw-overflow-hidden.tw-flex.tw-flex-column.tw-flex-grow-1.tw-flex-shrink-1 > div > div.chat-input.tw-pd-b-2.tw-pd-x-2 > div.tw-z-default.tw-relative > div:nth-child(1) > textarea')
time.sleep(40) #manually logging in because of captcha
print 'done sleep' #verification that the delay
chat.click()
while True:
num=(randint(0, 7))
if num==1:
chat.send_keys('1')
if num==2:
chat.send_keys('2')
if num==3:
chat._send_keys('3')
if num==4:
chat._send_keys('4')
if num==5:
chat._send_keys('5')
if num==6:
chat._send_keys('6')
if num==7:
chat._send_keys('7')
if keyboard.is_pressed('shift+q'):
break
driver.quit()
The error I get goes as follows:
Traceback (most recent call last):
File "C:\Users\Alex\Desktop\twitch_auto.py", line 11, in <module>
chat.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=65.0.3325.146)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
I'm aware I could use irc twitch but I got lost quick,so I stayed with what I had.
Thanks in advance.

The "chat" you had specified is the box that you type your message("done sleep").
You have to do the same process you did finding the chat element, but now with the button element that you have on bottom right. After you found your buttom element, just need to do buttom.click().

Related

WARNING: NEOS is temporarily unavailable. - Pyomo

I'm trying to learn how to send an optimization problem to NEOS Server to solve it with BARON, but I'm receiving an error which says NEOS is currently unavailable even though I've been trying it for some time. I've seen that there exists another question with the same error code, but it has not been resolved and people suspect it to be caused by using a proxy (Which I do not) therefore I opened another question.
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(initialize=1.5)
model.y = Var(initialize=1.5)
def rosenbrock(model):
return (1.0-model.x)**2 + 100.0*(model.y - model.x**2)**2
model.obj = Objective(rule=rosenbrock, sense=minimize)
solver_manager = SolverManagerFactory('neos')
results = solver_manager.solve(model, opt='baron')
results.write()
And the output is:
WARNING: NEOS is temporarily unavailable.
Traceback (most recent call last):
File "/Users/dorukeski/untitled/sa.py", line 15, in <module>
results = solver_manager.solve(model, opt='baron')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-
packages/pyomo/opt/parallel/async_solver.py", line 28, in solve
return self.execute(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-
packages/pyomo/opt/parallel/manager.py", line 119, in execute
ah = self.queue(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-
packages/pyomo/opt/parallel/manager.py", line 134, in queue
return self._perform_queue(ah, *args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-
packages/pyomo/neos/plugins/kestrel_plugin.py", line 128, in _perform_queue
raise ActionManagerError(
pyomo.opt.parallel.manager.ActionManagerError: Solver 'baron' is not recognized by NEOS.
Solver names recognized:
[]
Though this post is an old post.
I see someone suggested the fix in this link, https://github.com/Pyomo/pyomo/issues/2162
solution 1:
pip install --upgrade certifi
solution 2 if solution 1 not working:
1 - Open Internet Explorer as an Administrator (i.e., right-click on Internet Explorer and choose "Run as administrator")
2 - Navigate to https://neos-server.org
3 - Click on the lock icon
Click to view the image
4 - Click "View Certificate"
5 - Click "Install certificate..."
6 - Choose user or local machine, whatever is appropriate
7 - Select "Place all certificates in the following store" and choose "Trusted Root
8 - Certification Authorities"

google.api_core.exceptions.RetryError: on google-cloud-platform

Hi i am working on Google Cloud API, when i try to run the demo program given on GC documentation, for the following code
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
# Instantiates a client
client = language.LanguageServiceClient()
# The text to analyze
text = u'Hello, world!'
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
The code doesn't run when it hits this statement sentiment = client.analyze_sentiment(document=document).document_sentiment
and gives the following long error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/google/cloud/language_v1/gapic/language_service_client.py", line 247
, in analyze_sentiment
request, retry=retry, timeout=timeout, metadata=metadata
File "/usr/local/lib/python2.7/dist-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/google/api_core/retry.py", line 277, in retry_wrapped_func
on_error=on_error,
File "/usr/local/lib/python2.7/dist-packages/google/api_core/retry.py", line 202, in retry_target
last_exc,
File "/usr/lib/python2.7/dist-packages/six.py", line 718, in raise_from
raise value
google.api_core.exceptions.RetryError: Deadline of 600.0s exceeded while calling <functools.partial object at 0x7f9
527b06fc8>, last exception: 503 Getting metadata from plugin failed with error: ('Failed to retrieve http://metadat
a.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Engi
nemetadata service. Status: 404 Response:\n<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=vi
ewport content="initial-scale=1, minimum-scale=1, width=device-width">\n <title>Error 404 (Not Found)!!1</title>\n
<style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;paddi
ng:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.go
ogle.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{c
olor:#777;text-decoration:none}a img{border:0}#media screen and (max-width:772px){body{background:none;margin-top:0
;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_col
or_150x54dp.png) no-repeat;margin-left:-5px}#media only screen and (min-resolution:192dpi){#logo{background:url(//w
ww.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-im
age:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}#media only screen and (-w
ebkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_colo
r_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n
</style>\n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n <p><b>404.</b> <ins>That\xe2\
x80\x99s an error.</ins>\n <p>The requested URL <code>/computeMetadata/v1/instance/service-accounts/default/?recur
sive=true</code> was not found on this server. <ins>That\xe2\x80\x99s all we know.</ins>\n', <google.auth.transpor
t.requests._Response object at 0x7f9527b22710>)
how to i debug this error any help will be really appreciated, thank you
I’ve tried to replicate the issue but I was having a different error at my local test, since after the retry logic it failed due to a request timeout. Then I tried the same code at the cloud functions environment and it was working correctly.
My local env was the Cloud Shell, I’m afraid that this could be a problem from a recent outage in cloud networking.
To debug into your local environment you can use this tutorial.

when webdriver can’t find elem,Failed to establish a new connection [duplicate]

I have one question:I want to test "select" and "input".can I write it like the code below:
original code:
12 class Sinaselecttest(unittest.TestCase):
13
14 def setUp(self):
15 binary = FirefoxBinary('/usr/local/firefox/firefox')
16 self.driver = webdriver.Firefox(firefox_binary=binary)
17
18 def test_select_in_sina(self):
19 driver = self.driver
20 driver.get("https://www.sina.com.cn/")
21 try:
22 WebDriverWait(driver,30).until(
23 ec.visibility_of_element_located((By.XPATH,"/html/body/div[9]/div/div[1]/form/div[3]/input"))
24 )
25 finally:
26 driver.quit()
# #测试select功能
27 select=Select(driver.find_element_by_xpath("//*[#id='slt_01']")).select_by_value("微博")
28 element=driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/div[3]/input")
29 element.send_keys("杨幂")
30 driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/input").click()
31 driver.implicitly_wait(5)
32 def tearDown(self):
33 self.driver.close()
I want to test Selenium "select" function.so I choose sina website to select one option and input text in textarea.then search it .but when I run this test,it has error:
Traceback (most recent call last):
File "test_sina_select.py", line 32, in tearDown
self.driver.close()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 688, in close
self.execute(Command.CLOSE)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 376, in execute
return self._request(command_info[0], url, body=data)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 399, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 68, in request
**urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 81, in request_encode_url
return self.urlopen(method, url, **urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/poolmanager.py", line 247, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 597, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python2.7/site-packages/urllib3/util/retry.py", line 271, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
----------------------------------------------------------------------
Ran 1 test in 72.106s
who can tell me why?thanks
This error message...
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
...implies that the call to self.driver.close() method failed raising MaxRetryError.
A couple of things:
First and foremost as per the discussion max-retries-exceeded exceptions are confusing the traceback is somewhat misleading. Requests wraps the exception for the users convenience. The original exception is part of the message displayed.
Requests never retries (it sets the retries=0 for urllib3's HTTPConnectionPool), so the error would have been much more canonical without the MaxRetryError and HTTPConnectionPool keywords. So an ideal Traceback would have been:
ConnectionError(<class 'socket.error'>: [Errno 1111] Connection refused)
But again #sigmavirus24 in his comment mentioned ...wrapping these exceptions make for a great API but a poor debugging experience...
Moving forward the plan was to traverse as far downwards as possible to the lowest level exception and use that instead.
Finally this issue was fixed by rewording some exceptions which has nothing to do with the actual connection refused error.
Solution
Even before self.driver.close() within tearDown(self) is invoked, the try{} block within test_select_in_sina(self) includes finally{} where you have invoked driver.quit()which is used to call the /shutdown endpoint and subsequently the web driver & the client instances are destroyed completely closing all the pages/tabs/windows. Hence no more connection exists.
You can find a couple of relevant detailed discussion in:
PhantomJS web driver stays in memory
Selenium : How to stop geckodriver process impacting PC memory, without calling
driver.quit()?
In such a situation when you invoke self.driver.close() the python client is unable to locate any active connection to initiate a clousure. Hence you see the error.
So a simple solution would be to remove the line driver.quit() i.e. remove the finally block.
tl; dr
As per the Release Notes of Selenium 3.14.1:
* Fix ability to set timeout for urllib3 (#6286)
The Merge is: repair urllib3 can't set timeout!
Conclusion
Once you upgrade to Selenium 3.14.1 you will be able to set the timeout and see canonical Tracebacks and would be able to take required action.
References
A couple of relevent references:
Adding max_retries as an argument
Removed the bundled charade and urllib3.
Third party libraries committed verbatim
Just had the same problem. The solution was to change the owner of the folder with a script recursively. In my case the folder had root:root owner:group and I needed to change it to ubuntu:ubuntu.
Solution: sudo chown -R ubuntu:ubuntu /path-to-your-folder
Use Try and catch block to find exceptions
try:
r = requests.get(url)
except requests.exceptions.Timeout:
#Message
except requests.exceptions.TooManyRedirects:
#Message
except requests.exceptions.RequestException as e:
#Message
raise SystemExit(e)

Selenium stopped working with Firefox, works with Safari

I've been using Selenium with Python 3 for a while. Yesterday it stopped working. I've tried uninstalling and reinstalling everything, but it hasn't worked. I enabled the driver for Safari, and that works, but I don't know what changed. I tried going to a different project that I haven't used in a while but functioned, thinking my code threw it off, but it doesn't work either. I know there are other posts out there similar to this, but they haven't been helpful and I wasn't sure if I should contact Mozilla/Selenium about this issue. Here are my details:
Python 3.7.1
Django 2.1
Firefox 64
Selenium 3.141.0
Geckodriver 0.23.0
I am running these in a virtual environment. Any more questions, let me know. Really confused as to what broke.
This is all of the code related to launching the browser:
class FunctionalTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def get_page(self, url_extention, window=None):
window_size = [900, 700]
if window == 'xs':
window_size = [500, 700]
elif window == 'sm':
window_size = [700, 500]
elif window == 'md':
window_size = [900, 700]
elif window == 'lg':
window_size = [1024, 768]
elif window == 'xl':
window_size = [1400, 800]
return (
self.browser.get(
self.live_server_url + url_extention
),
self.browser.set_window_size(
window_size[0],
window_size[1]
)
)
class PageFunctionTest(FunctionalTest):
def test_customer_page(self):
self.get_page('/customer/profile/1/', window='xs')
# I also tried this just to check:
self.browser.get(self.live_server_url)
Basically the browser opens, throws an error and then quits. This is what shows in the browser window:
address bar: http//localhost:59905/customer/profile/1/
browser error: Firefox can’t establish a connection to the server at localhost:59905.
And this is the traceback:
Traceback (most recent call last): File
"tests/functional_tests/test_function_profile_update_phone_number.py",
line 21, in test_update_profile_phone_number
self.get_page(f'/customer/profile/1/', window='xs') File "/base_tests/base_FunctionalTest.py", line 70, in get_page
self.live_server_url + url_extention File "/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 333, in get
self.execute(Command.GET, {'url': url}) File "/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 321, in execute
self.error_handler.check_response(response) File "/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: Reached error
page:
about:neterror?e=connectionFailure&u=http%3A//localhost%3A59905/customer/profile/1/&c=UTF-8&f=regular&d=Firefox%20can%E2%80%99t%20establish%20a%20connection%20to%20the%20server%20at%20localhost%3A59905.

How to take multiple screenshots through Selenium in Python?

GECKODRIVER_PATH = 'F:/geckodriver.exe'
firefox_options = Options()
firefox_options .add_argument("-headless")
driver = webdriver.Firefox(executable_path=CHROMEDRIVER_PATH, firefox_options = firefox_options )
test = []
test.append('http://google.com')
test.append('http://stackoverflow.com')
for x in test:
print x
driver.get(x)
driver.set_page_load_timeout(20)
filename = str(x)+'.png'
driver.save_screenshot( filename )
driver.close()
Now, how can I take multiple screenshots and save them in the different filename? As you can see I am trying to save the filename according to domain URL but failed.
See the error below:
http://google.com
http://card.com
Traceback (most recent call last):
File "F:\AutoRecon-master\test.py", line 125, in <module>
driver.get(x)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 326, in get
self.execute(Command.GET, {'url': url})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Tried to run command without establishing a connection
Can anyone please tell me what is the exact problem is? Will be a big help.
Try to move driver.close() out of loop:
for x in test:
print x
driver.get(x)
driver.set_page_load_timeout(20)
filename = str(x)+'.png'
driver.save_screenshot( filename )
driver.close()
Also note that x is already a string, so there is no need in str(x)
P.S. I'm not sure that http://stackoverflow.com.png filename is acceptable, you might need to use:
filename = x.split('//')[-1] + '.png'
Tried to run command without establishing a connection
you are closing the browser within your for loop... so the 2nd time through the loop it fails with the error above (since the browser is closed, the connection to geckodriver has already been terminated).
other issues:
you are setting the page_load_timeout after you have already fetched the page, so it is not doing anything useful.
using CHROMEDRIVER_PATH as the name for Geckodriver is just confusing. Chromedriver is not used at all here.