Saya menemukan Chrome tanpa kepala sepertinya tidak mendeteksi elemen. Skrip berikut tidak berfungsi.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui  import WebDriverWait

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

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
print('hi')

Ubah menjadi tidak tanpa kepala.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
print('test2')

Keberhasilan!

Saya mendapatkan untuk tanpa kepala:

line 49, in <module>
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
  File "C:\Users\Django\AppData\Local\Continuum\miniconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Ubah css: div:nth-child(1) > a > span.team-name.ib

Saya masih mendapatkan kesalahan yang sama. Mengapa melakukan ini?

5
user9045698 20 Desember 2017, 10:05

2 jawaban

Jawaban Terbaik

Tampaknya mengarahkan Anda jika Anda menggunakan browser tanpa kepala.

Menambahkan:

user_agent = 'I LIKE CHOCOLATE'
options.add_argument(f'user-agent={user_agent}')

Sekarang jalankan:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui  import WebDriverWait

options = Options()
options.add_argument("--headless")
user_agent = 'I LIKE CHOCOLATE'
options.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(1024, 600)
options.add_argument("--headless");
driver.maximize_window()

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
print('hi')

KESUKSESAN!

7
user9045698user9045698 24 Desember 2017, 07:53

Konfigurasi yang disebutkan di bawah memecahkan masalah saya.

    final ChromeOptions options = new ChromeOptions();
    options.addArguments("--window-size=1920,1080");
    options.addArguments("--allow-insecure-localhost");
    options.addArguments("--headless");
    options.addArguments("--disable-gpu");
    options.addArguments("--no-sandbox");
    DesiredCapabilities caps = DesiredCapabilities.chrome();
    caps.setCapability(ChromeOptions.CAPABILITY, options);
    caps.setCapability("acceptInsecureCerts", caps);
    WebDriver driver = new ChromeDriver(options);
1
Mahadev Gouda 12 September 2020, 07:21