作者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://webptt.com/cn.aspx?n=bbs/Python/M.1570938800.A.6F2.html
※ 编辑: poototo (123.193.43.58 台湾), 10/13/2019 12:07:21
1F:→ poototo: 加 chrome_options.add_argument('user-agent=Mozilla... 10/13 15:32
2F:→ poototo: 使用者代理可解决 10/13 15:34
3F:推 rexyeah: 一般遇到抓不到我都先印page source看看长怎样 10/14 15:59
4F:推 Falldog: 确定元素在可见范围内吗? 10/16 00:20