Chrome 122.0.6261.95
Chrome 驱动程序 122.0.6261.94
Python 3.8.3
如果我注释掉 option.add_argument("--headless=new"),它将 print(len(elements)) 打印 2。否则,无法打印任何内容。 非常感谢
import time
import subprocess
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
import pandas as pd
#from playsound import playsound
import datetime
import threading
service = Service(executable_path='chromedriver.exe')
option = webdriver.ChromeOptions()
option.add_argument("--headless=new")
option.add_argument('--ignore-certificate-errors')
option.add_argument("--no-sandbox")
option.add_argument('disable-notifications')
driver = webdriver.Chrome(service=service,options=option)
def search(dep,arr,date):
print(f'''Input: Date:{date},Departure: {dep} - Arrival: {arr}''')
base_url = f'https://www.kayak.com/flights/SFO-PEK/2024-03-06?sort=price_a&fs=stops=0'
print("before webdriver.ChromeOptions()")
my_url = base_url
driver.get(my_url)
print(my_url)
time.sleep(10) # set the time to wait till web fully loaded
# Find all elements with class "nrc6"
elements = driver.find_elements(By.CLASS_NAME, "nrc6")
# Print the number of elements found
print(len(elements))
请更新遇到错误的问题,以便我们解决。
可能是由于您的chromedriver和浏览器版本不兼容。
我通常使用 WebDriverManager,这样我就不需要跟踪最新的和不是最新的。
随意尝试使用这个插件:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.6.4</version>
</dependency>
您的代码会略有不同,因为您需要首先声明 WebDriverManager。 https://pypi.org/project/webdriver-manager