Headless Chrome 未加载页面

Headless Chrome not loading page

Headless Chrome 无法加载页面并卡在:

wait = WebDriverWait(driver, 30)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".price-link:nth-child(1) .team-name")))

为什么要这样做?这是一个错误吗,因为它在正常情况下完美运行 chrome 并在下面打印 h 除了无头...

from selenium import webdriver
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.webdriver.chrome.options import Options


options = Options()
options.add_argument("--headless")
options.add_argument("window-size=1400,600")
driver = webdriver.Chrome(chrome_options=options)
#driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')
print('?')
wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".price-link:nth-child(1) .team-name")))

print('h')

另一个例子:

from selenium import webdriver
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.webdriver.chrome.options import Options


options = Options()
options.add_argument("--headless")
options.add_argument("window-size=1400,600")
driver = webdriver.Chrome(chrome_options=options)
#driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()
import time
driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')
import time
time.sleep(10)

langs = driver.find_elements_by_css_selector(".price-link:nth-child(1) .team-name")
langs_text = []

for lang in langs:
    print(lang.text)
    langs_text.append(lang.text)

print('h')

作业打印时删除了 wait until 尽管没有任何内容被刮掉。页面未加载可能是问题。

尝试使用以下代码:

element = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".price-link:nth-child(1) .team-name")))

driver.execute_script("arguments[0].scrollIntoView(true);", element)

希望对您有所帮助!