diplomatgmg
@diplomatgmg
Делаю что-то гениальное

Как открыть больше 20 вкладок в selenium?

не открывается больше 20 вкладок, как это можно решить?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options

url = 'https://www.avito.ru/sankt-peterburg/noutbuki?cd=1'

options = Options()
options.page_load_strategy = 'eager'
driver = webdriver.Firefox(options=options)

driver.get(url)
count_pages_old = int(driver.find_elements(By.CLASS_NAME, 'pagination-item-JJq_j')[-2].text)
driver.get(f'{url}&p={count_pages_old}')

count_pages = int(driver.find_elements(By.CLASS_NAME, 'pagination-item-JJq_j')[-2].text)
print(count_pages)

for num_page in range(1, count_pages + 2):
    print(f'Открываем страницу {num_page}')
    driver.execute_script(f"window.open('about:blank', 'page{num_page}');")
    driver.switch_to.window(f'page{num_page}')
    driver.get(f"{url}&p={num_page}")


Traceback
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 143, in _w3c_window
    send_handle(window_name)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 139, in send_handle
    self._driver.execute(Command.SWITCH_TO_WINDOW, {'handle': h})
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to locate window: page4
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:192:5
NoSuchWindowError@chrome://remote/content/shared/webdriver/Errors.jsm:446:5
GeckoDriver.prototype.switchToWindow@chrome://remote/content/marionette/driver.js:1226:11
despatch@chrome://remote/content/marionette/server.js:304:40
execute@chrome://remote/content/marionette/server.js:277:16
onPacket/<@chrome://remote/content/marionette/server.js:250:20
onPacket@chrome://remote/content/marionette/server.js:251:9
_onJSONObjectReady/<@chrome://remote/content/marionette/transport.js:502:20
permitUnload@chrome://global/content/elements/browser-custom-element.js:1681:21
CanCloseWindow@chrome://browser/content/browser.js:7980:36
WindowIsClosing@chrome://browser/content/browser.js:8009:7
BrowserTryToCloseWindow@chrome://browser/content/browser.js:3031:22
oncommand@chrome://browser/content/browser.xhtml:1:24


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\1 Python\work\projects\main.py", line 21, in <module>
    driver.switch_to.window(f'page{num_page}')
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 134, in window
    self._w3c_window(window_name)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 149, in _w3c_window
    send_handle(handle)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 139, in send_handle
    self._driver.execute(Command.SWITCH_TO_WINDOW, {'handle': h})
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to locate window: f24006de-3a5b-45ab-9e32-5a3eb239eca5
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:192:5
NoSuchWindowError@chrome://remote/content/shared/webdriver/Errors.jsm:446:5
GeckoDriver.prototype.switchToWindow@chrome://remote/content/marionette/driver.js:1226:11
despatch@chrome://remote/content/marionette/server.js:304:40
execute@chrome://remote/content/marionette/server.js:277:16
onPacket/<@chrome://remote/content/marionette/server.js:250:20
onPacket@chrome://remote/content/marionette/server.js:251:9
_onJSONObjectReady/<@chrome://remote/content/marionette/transport.js:502:20
observe@resource://gre/modules/AsyncShutdown.jsm:576:16


Process finished with exit code 1
  • Вопрос задан
  • 270 просмотров
Пригласить эксперта
Ответы на вопрос 1
diplomatgmg
@diplomatgmg Автор вопроса
Делаю что-то гениальное
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options

url = 'https://www.avito.ru/sankt-peterburg/noutbuki?cd=1'

options = Options()
options.page_load_strategy = 'eager'
driver = webdriver.Firefox(options=options)

driver.get(url)
count_pages_old = int(driver.find_elements(By.CLASS_NAME, 'pagination-item-JJq_j')[-2].text)
driver.get(f'{url}&p={count_pages_old}')

count_pages = int(driver.find_elements(By.CLASS_NAME, 'pagination-item-JJq_j')[-2].text)

original_window = driver.current_window_handle

print(f'Создаем {count_pages} вкладок')
for num_page in range(1, count_pages + 2):
    driver.switch_to.new_window('tab')


page = 1
for window_handle in driver.window_handles:
    driver.switch_to.window(window_handle)
    print(f'Открываем страницу {page}')
    driver.get(f"{url}&p={page}")
    page += 1
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы