我想在 twitter 上做一些操作,但是一旦程序接近我想调用/查找元素的代码行末尾,例如主页上的评论输入,程序就会结束并关闭下一条错误消息:
selenium.common.exceptions.NoSuchWindowException:消息:没有这样的窗口:窗口已经关闭
几天前我更新了我的 chrome 驱动程序和 chrome 浏览器,所以现在它们是匹配的。-
这是整个代码:
import random
import undetected_chromedriver as u_c
import time
from selenium.webdriver.common.by import By
MINIMUM_DOWN = 40
MINIUMM_UP = 10
CHROME_DRIVER_PATH = r'C:\Development\chromedriver.exe'
TWITTER_PASSWORD = 'ya>xs#cd&'
TWITTER_EMAL = 'hubokjaros@gmail.com'
TWITTER_URL = 'https://twitter.com/home'
INTERNET_SPEED_URL = 'https://www.speedtest.net/de/result/12840864881'
random_num = random.randrange(2,3)
upload = 0
download = 0
class InternetSpeedTwitterBot:
def __init__(self):
self.driver = u_c.Chrome()
self.up = MINIUMM_UP
self.down = MINIMUM_DOWN
def wait(self):
self.driver.implicitly_wait(random_num)
time.sleep(random_num)
print('waited')
def get_internet_speed(self):
global upload, download
self.driver.get(INTERNET_SPEED_URL)
self.wait()
try:
self.driver.find_element(by=By.XPATH, value='/html/body/div[6]/div[2]/div[1]/div[5]/button[2]').click()
except:
pass
self.wait()
time.sleep(2)
self.los_button = self.driver.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div/div/div/div['
'2]/div[3]/''div[1]/a').click()
self.driver.implicitly_wait(50)
time.sleep(50)
self.download_speed = self.driver.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div/div/div/'
'div[2]/div[3]/div[3]/div/div[3]/div'
'/div/div[2]/div[1]/div[2]/div/div[2]/span')
self.upload_speed = self.driver.find_element(by=By.XPATH,value='/html/body/div[3]/div/div[3]/div/div/div/div'
'[2]/div[3]''/div[3]/div/div[3]/div/div/div[2]'
'/div[1]/div[3]/div/div[2]/span')
print(f'download speed:{self.download_speed.text}\nupload speed:{self.upload_speed.text}')
time.sleep(1)
download = float(self.download_speed.text.strip())
print(download)
print(type(download))
upload = float(self.upload_speed.text.strip())
print(upload)
print(type(upload))
def tweet_at_provider(self):
self.driver.get(TWITTER_URL)
self.wait()
self.wait()
before_window = self.driver.window_handles[0]
self.wait()
iframe = self.driver.find_element(By.XPATH, value='/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/'
'div/div/div[2]/div[2]/div[1]/div/div/div[2]/div/iframe')
self.wait()
self.driver.switch_to.frame(iframe)
print('switched')
login_label = self.driver.find_element(By.ID, value='container').click()
self.wait()
after_window = self.driver.window_handles[1]
self.wait()
self.driver.switch_to.window(after_window)
self.wait()
self.driver.execute_script(f"document.getElementById('identifierId').value='{TWITTER_EMAL}'")
self.wait()
self.driver.find_element(By.XPATH, value='/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/d'
'iv/div[2]/div/div[1]/div/div/button').click()
self.wait()
self.driver.execute_script(f"document.getElementsByName('password')[0].value='{TWITTER_PASSWORD}'")
self.wait()
self.driver.find_element(By.XPATH, value='/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[2]'
'/div/div[1]/div/div/button')
self.wait()
self.wait()
# self.driver.switch_to(self.driver.window_handles[0])
self.driver.find_element(By.XPATH, value='/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]'
'/div/div[2]/div/div[1]/div/div/button').click()
self.wait()
self.wait()
# self.driver.find_element(By.XPATH, value='/html/body/div[1]/div/div/div[1]/div/div/div/div/div/div[2]/div[1]/div').click()
# self.driver.execute_script(f"document.getElementsByClassName('public-DraftStyleDefault-block public-DraftStyleDefault-ltr')[0].value='{TWITTER_PASSWORD}'")
# self.driver.find_element(By.CLASS_NAME, value='public-DraftStyleDefault-block public-DraftStyleDefault-ltr').send_keys('LOVE')
layers_id = self.driver.execute_script("document.getElementById('layers')")
print(layers_id)
time.sleep(10000)
if __name__ == '__main__':
twitter_bot = InternetSpeedTwitterBot()
# twitter_bot.get_internet_speed()
# if upload < MINIUMM_UP or download < MINIMUM_DOWN:
# twitter_bot.tweet_at_provider()
# else:
# print('all good!')
twitter_bot.wait()
twitter_bot.tweet_at_provider()
这是整个错误消息:
*Traceback (most recent call last):
File "C:\Users\gergr\PycharmProjects\twitter_complain_bot\main.py", line 112, in <module>
twitter_bot.tweet_at_provider()
File "C:\Users\gergr\PycharmProjects\twitter_complain_bot\main.py", line 99, in tweet_at_provider
layers_id = self.driver.execute_script("document.getElementById('layers')")
File "C:\Users\gergr\PycharmProjects\twitter_complain_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 879, in execute_script
return self.execute(command, {
File "C:\Users\gergr\PycharmProjects\twitter_complain_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "C:\Users\gergr\PycharmProjects\twitter_complain_bot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: window was already closed
(Session info: chrome=99.0.4844.51)
Stacktrace:
Backtrace:
Ordinal0 [0x00F69943+2595139]
Ordinal0 [0x00EFC9F1+2148849]
Ordinal0 [0x00DF4528+1066280]
Ordinal0 [0x00DE97C7+1021895]
Ordinal0 [0x00DEA892+1026194]
Ordinal0 [0x00DE4219+999961]
Ordinal0 [0x00DF5860+1071200]
Ordinal0 [0x00E4B2D2+1422034]
Ordinal0 [0x00E3B806+1357830]
Ordinal0 [0x00E16086+1204358]
Ordinal0 [0x00E16F96+1208214]
GetHandleVerifier [0x0110B232+1658114]
GetHandleVerifier [0x011C312C+2411516]
GetHandleVerifier [0x00FFF261+560433]
GetHandleVerifier [0x00FFE366+556598]
Ordinal0 [0x00F0286B+2173035]
Ordinal0 [0x00F075F8+2192888]
Ordinal0 [0x00F076E5+2193125]
Ordinal0 [0x00F111FC+2232828]
BaseThreadInitThunk [0x75C26739+25]
RtlGetFullPathName_UEx [0x76F18E7F+1215]
RtlGetFullPathName_UEx [0x76F18E4D+1165]
Process finished with exit code 1*