質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

1261閲覧

sysが原因でエラーがでる

makamaka

総合スコア21

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2021/09/17 11:51

前提・実現したいこと

抽選サイトで抽選可能なものに申し込みを自動で行うシステムを作りたいと思っています。
抽選できるものには申し込みボタンがあり、自動で申し込みボタンを見つけ、クリックするシステムを組みたいと思っています。
1か所につき申し込みは一度まで、常に抽選があるのではなく、抽選がない場合には「不可」を出力され、システムを閉じるようにしたいです。

pythonのseleniumを使い、途中でつまずいてしまったため、解決方法をご教授いただきたいです。
おそらくsys.exit()が邪魔をしているのではないかと考えていますが、解決策がわかりません。

発生している問題・エラーメッセージ

--------------------------------------------------------------------------- NoSuchElementException Traceback (most recent call last) <ipython-input-38-ec29e3e3dcb7> in <module> 2 try: ----> 3 element = browser.find_element_by_xpath('//img[@alt=\"申込\"]') 4 except: C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath) 393 """ --> 394 return self.find_element(by=By.XPATH, value=xpath) 395 C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value) 975 value = '[name="%s"]' % value --> 976 return self.execute(Command.FIND_ELEMENT, { 977 'using': by, C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params) 320 if response: --> 321 self.error_handler.check_response(response) 322 response['value'] = self._unwrap_value( C:\anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response) 241 raise exception_class(message, screen, stacktrace, alert_text) --> 242 raise exception_class(message, screen, stacktrace) 243 NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt="申込"]"} (Session info: chrome=93.0.4577.82) During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) <ipython-input-38-ec29e3e3dcb7> in <module> 5 print('不可') 6 browser.quit() ----> 7 sys.exit() NameError: name 'sys' is not defined

該当のソースコード

pyhon
try: element = browser.find_element_by_xpath('//img[@alt=\"申込\"]') except: print('不可') browser.quit() sys.exit()

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2021/09/17 12:11

> おそらくsys.exit()が邪魔をしているのではないかと考えていますが、解決策がわかりません。 ご自身で書かれたコードではないんでしょうか?
makamaka

2021/09/17 14:54

私が書いたコードなのですが、拝見したサイトにはsys.exit()でシステムが閉じるとなっていました。browser.quit()まではうまく起動していたのでsys.exit()が原因なのではないかと考えました。
guest

回答1

0

ベストアンサー

sys.exit()

をするときは、その前に

import sys

が必要です。

投稿2021/09/17 12:24

ppaul

総合スコア24672

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

makamaka

2021/09/17 14:58

ご回答ありがとうございました。 import sysが必要だったのですね知りませんでした。 教えていただきありがとうございます。 ちなみに、import sysをすると、追記のように出てしまうのですが、対応の仕方についてわかる範囲内で結構ですのでご教授いただけないでしょうか。
makamaka

2021/09/17 14:59

ERROR:root:Internal Python error in the inspect module. Below is the traceback from this internal error.
makamaka

2021/09/17 15:00

Traceback (most recent call last): File "<ipython-input-60-ec29e3e3dcb7>", line 3, in <module> element = browser.find_element_by_xpath('//img[@alt=\"申込\"]') File "C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt="申込"]"} (Session info: chrome=93.0.4577.82) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-60-ec29e3e3dcb7>", line 7, in <module> sys.exit() SystemExit During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\anaconda\lib\site-packages\IPython\core\ultratb.py", line 1101, in get_records return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) File "C:\anaconda\lib\site-packages\IPython\core\ultratb.py", line 248, in wrapped return f(*args, **kwargs) File "C:\anaconda\lib\site-packages\IPython\core\ultratb.py", line 281, in _fixed_getinnerframes records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) File "C:\anaconda\lib\inspect.py", line 1515, in getinnerframes frameinfo = (tb.tb_frame,) + getframeinfo(tb, context) AttributeError: 'tuple' object has no attribute 'tb_frame'
makamaka

2021/09/17 15:00

-------------------------------------------------------------------------- NoSuchElementException Traceback (most recent call last) <ipython-input-60-ec29e3e3dcb7> in <module> 2 try: ----> 3 element = browser.find_element_by_xpath('//img[@alt=\"申込\"]') 4 except: C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath) 393 """ --> 394 return self.find_element(by=By.XPATH, value=xpath) 395 C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value) 975 value = '[name="%s"]' % value --> 976 return self.execute(Command.FIND_ELEMENT, { 977 'using': by, C:\anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params) 320 if response: --> 321 self.error_handler.check_response(response) 322 response['value'] = self._unwrap_value( C:\anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response) 241 raise exception_class(message, screen, stacktrace, alert_text) --> 242 raise exception_class(message, screen, stacktrace) 243 NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt="申込"]"} (Session info: chrome=93.0.4577.82) During handling of the above exception, another exception occurred: SystemExit Traceback (most recent call last) [... skipping hidden 1 frame] <ipython-input-60-ec29e3e3dcb7> in <module> 6 browser.quit() ----> 7 sys.exit() SystemExit: During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) [... skipping hidden 1 frame] C:\anaconda\lib\site-packages\IPython\core\interactiveshell.py in showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code) 2052 stb = ['An exception has occurred, use %tb to see ' 2053 'the full traceback.\n'] -> 2054 stb.extend(self.InteractiveTB.get_exception_only(etype, 2055 value)) 2056 else: C:\anaconda\lib\site-packages\IPython\core\ultratb.py in get_exception_only(self, etype, value) 752 value : exception value 753 """ --> 754 return ListTB.structured_traceback(self, etype, value) 755 756 def show_exception_only(self, etype, evalue): C:\anaconda\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, evalue, etb, tb_offset, context) 627 chained_exceptions_tb_offset = 0 628 out_list = ( --> 629 self.structured_traceback( 630 etype, evalue, (etb, chained_exc_ids), 631 chained_exceptions_tb_offset, context) C:\anaconda\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context) 1365 else: 1366 self.tb = tb -> 1367 return FormattedTB.structured_traceback( 1368 self, etype, value, tb, tb_offset, number_of_lines_of_context) 1369 C:\anaconda\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context) 1265 if mode in self.verbose_modes: 1266 # Verbose modes need a full traceback -> 1267 return VerboseTB.structured_traceback( 1268 self, etype, value, tb, tb_offset, number_of_lines_of_context 1269 ) C:\anaconda\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context) 1122 """Return a nice text document describing the traceback.""" 1123 -> 1124 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, 1125 tb_offset) 1126 C:\anaconda\lib\site-packages\IPython\core\ultratb.py in format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset) 1080 1081 -> 1082 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) 1083 1084 frames = self.format_records(records, last_unique, recursion_repeat) C:\anaconda\lib\site-packages\IPython\core\ultratb.py in find_recursion(etype, value, records) 380 # first frame (from in to out) that looks different. 381 if not is_recursion_error(etype, value, records): --> 382 return len(records), 0 383 384 # Select filename, lineno, func_name to track frames with TypeError: object of type 'NoneType' has no len()
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問