我正在尝试运行脚本,但是当我运行进程时冻结了。这是代码
def get_source_content(url):
driver_path = f"{settings.BASE_DIR}/geckodriver"
options = FirefoxOptions()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--single-process")
options.add_argument("--ignore-certificate-errors")
driver = webdriver.Firefox(service=FirefoxService(executable_path=driver_path), options=options)
try:
driver.get(url)
WebDriverWait(driver, 3)
element = driver.find_element(
"xpath", "//button[@class='sc-beySPh gNAvzR mde-consent-accept-btn']"
)
element.click()
WebDriverWait(driver, 3)
source = driver.page_source
except Exception as ex:
raise ex
driver.quit()
return source
代码仅冻结部署在AWS EC2上,本地运行良好。
您的问题是您对 WebDriverWait 的使用。它不应该这样使用(这样它实际上什么也不做)。一般用法是WebDriverWait(driver, amount).until(expected_conditions.some_condition(webelement))
您的情况:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//button[@class='sc-beySPh gNAvzR mde-consent-accept-btn']")))