作者poototo (poototo)
看板Python
標題[問題] selenium headless抓不到元素
時間Sun Oct 13 11:53:17 2019
各位版友好:
練習抓NBA官網的球員數據table
開啟GUI時,OK,win10,chrome 77
一加--headless,就抓不到,不知是否有其他解? Thx
ps:沒有GUI時,感覺問題很多?
--------------------------------------------------------------
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import
visibility_of_element_located
from selenium.webdriver.common.by import By
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--headless') #不用打开图形界面
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox') #让Chrome在root权限下跑
chrome_options.add_argument('--proxy-bypass-list=*')
chrome_options.add_argument("--proxy-server='direct://'")
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--disable-useAutomationExtension')
#
driver = webdriver.Chrome("./chromedriver.exe",options=chrome_options)
driver.implicitly_wait(15)
driver.get("
http://stats.nba.com/players/traditional/?sort=PTS&dir=-1")
xpath = '//table[1]'
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,
xpath)))
tables=driver.find_elements_by_css_selector("table")
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.43.58 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1570938800.A.6F2.html
※ 編輯: poototo (123.193.43.58 臺灣), 10/13/2019 12:07:21
→ poototo: 加 chrome_options.add_argument('user-agent=Mozilla... 10/13 15:32
→ poototo: 使用者代理可解決 10/13 15:34
推 rexyeah: 一般遇到抓不到我都先印page source看看長怎樣 10/14 15:59
推 Falldog: 確定元素在可見範圍內嗎? 10/16 00:20