[問題] selenium headless send_Keys
各位版友好,
小弟爬蟲學了一段時間,但這個問題卡了很久..
我的目標是:我想利用selenium + chromedriver爬取匯率網頁,並且透過send_Keys
去改變日期,得到該天的匯率資料。
舉例來說,我輸入2019-09-06,USD-EUR的平均賣出價是0.90588
如果沒有headless時,send_Keys是正常的,selenium可以抓到0.90588
但如果有headless的情況時,send_Keys無法運作,date不會變,只能抓到今日的0.90721
因為未來程式會放在linux server上,一定得要加上headless,
但此時send_Keys出了問題,不知道是否有解?先謝謝各位的幫忙,thanks!!
程式碼如下:
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd
from selenium.webdriver.common.keys import Keys
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--window-size=1920x1080')
chrome_options.add_argument('--disable-gpu')
chrome_path = 'D://work/chromedriver.exe'
driver = webdriver.Chrome(chrome_path,chrome_options=chrome_options)
#start to crawl
driver.get('https://www1.oanda.com/currency/converter/')
time.sleep(1)
end_date_input = driver.find_element_by_id('end_date_input')
end_date_input.send_keys(Keys.CONTROL + 'a')
time.sleep(1)
end_date_input.send_keys(Keys.DELETE)
time.sleep(1)
end_date_input.send_keys('2019-09-06')
time.sleep(1)
end_date_input.send_keys(Keys.ENTER)
time.sleep(1)
soup = BeautifulSoup(driver.page_source, 'html.parser')
table_soup= soup.findAll('tr',{'class':'body'})[1]
bidAskAskAvg = round(float(table_soup.findAll('td')[4].text),8)
print(bidAskAskAvg)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.49.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1567942701.A.263.html
→
09/08 19:54,
5年前
, 1F
09/08 19:54, 1F
推
09/08 21:48,
5年前
, 2F
09/08 21:48, 2F
→
09/08 21:48,
5年前
, 3F
09/08 21:48, 3F
→
09/09 00:06,
5年前
, 4F
09/09 00:06, 4F
Python 近期熱門文章
PTT數位生活區 即時熱門文章