我正在尝试通过以下链接下载 pdf 文件: https://www.cms.gov/medicare-coverage-database/view/lcd.aspx?lcdid=39763&ver=8
此网站上的大多数文档都会弹出接受许可证的窗口,我可以使用它关闭
driver.find_element(By.ID, "btnAcceptLicense").click()
上面的链接随后会弹出另一个包含“关闭”按钮的窗口。我可以找到
elem = driver.find_element(By.CLASS_NAME, "modal-footer").find_element(By.CLASS_NAME, "btn")
但是当我运行 elem.click()
时,我得到了 ElementNotInteractableException
。
这是我到目前为止所写的内容:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
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)
url = "https://www.cms.gov/medicare-coverage-database/view/lcd.aspx?lcdid=39859&ver=4"
driver.get(url)
time.sleep(0.5)
# Accept license
driver.find_element(By.ID, "btnAcceptLicense").click()
time.sleep(0.5)
# Close pop second pop up (fails here)
driver.find_element(By.CLASS_NAME, "modal-footer").find_element(By.CLASS_NAME, "btn").click()
# Download document
driver.find_element(By.ID, "btnDownload").click()
尝试使用 XPath 来单击关闭按钮:
#关闭第二个弹出窗口(此处失败)<<- replace the line after this, with: driver.find_element(By.XPATH,"/html/body/main/form/div[3]/div[7]/div/div/div[3]/button").click()