Тайм-аут происходит в driver.get(url). Как ни странно, код работает до тех пор, пока драйвер ранее не посещал другой URL-адрес. Например, в приведенном ниже коде not_working_url успешно получит веб-страницу и загрузит файл, если он не идет после work_url.
Код: Выделить всё
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
def download_documents() -> None:
"""Download billing code documents from CMS"""
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=chrome_options)
working_url = "https://www.cms.gov/medicare-coverage-database/view/article.aspx?articleid=59626&ver=6"
not_working_url = "https://www.cms.gov/medicare-coverage-database/view/lcd.aspx?lcdid=36377&ver=19"
for row in [working_url, not_working_url]:
print(f"Retrieving from {row}...")
driver.get(row) # Fails on second url
print("Wait for webdriver...")
wait = WebDriverWait(driver, 2)
print("Attempting license accept...")
# Accept license
try:
wait.until(EC.element_to_be_clickable((By.ID, "btnAcceptLicense"))).click()
except TimeoutException:
pass
wait = WebDriverWait(driver, 4)
print("Attempting pop up close...")
# Click on Close button of the second pop-up
try:
wait.until(
EC.element_to_be_clickable(
(
By.XPATH,
"//button[@data-page-action='Clicked the Tracking Sheet Close button.']",
)
)
).click()
except TimeoutException:
pass
print("Attempting download...")
driver.find_element(By.ID, "btnDownload").click()
download_documents()
Потенциально связанная проблема: исключение Selenium TimeoutException в Google Colab
Подробнее здесь: https://stackoverflow.com/questions/791 ... ogle-colab
Мобильная версия