无头Chrome无法加载页面

时间:2017-12-21 12:43:11

标签: python selenium selenium-chromedriver headless

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

wait = WebDriverWait(driver, 30)

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

为什么这样做?这是一个错误,因为它在普通镀铬中完美地工作并且在下面打印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')

作业打印时已删除等待,直到没有任何内容被删除。页面未加载可能是问题。

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码:

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)

希望它可以帮到你!