我发现我可以对其进行修改以适应我的需求。我想在一些名为“ ticker”的清单中添加一些股票,这些股票确实包含25个股票代码,同时也有一个包含股票行情清单的股票行情清单。 问题是,对于每个股票行情清单,调用函数“ def find_fa_sheet”的for循环仅运行2至3次,然后前进到下一个股票行情清单,并且在我的错误日志文件中,我得到警告:消息:元素不存在互动的。而且我认为这与我的Selenium部分有关,任何可以提供帮助的人。
import time
from random import randint
from PIL import Image
from io import BytesIO
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
def open_url():
# with webdriver.Chrome() as driver:
# wait = WebDriverWait(driver, 10)
# driver.get("https://www.stockopedia.com/auth/login/")
# driver.find_element_by_name("username").send_keys("johnnyhjorsal@gmail.com")
# driver.find_element_by_name("password").send_keys("Salis321" + Keys.RETURN)
# first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
# print(first_result.get_attribute("textContent"))
options = Options()
options.headless = True
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()
driver.get('https://www.stockopedia.com/auth/login/')
driver.find_element_by_id('username').send_keys('xxxxxxxxxxxxxx')
driver.find_element_by_id('password').send_keys('xxxxxxxx')
driver.find_element_by_id('auth_submit').click()
time.sleep(randint(15, 20))
return driver
def find_fa_sheet(driver, ticker):
# Indsæt tickerkode
driver.find_element_by_name('searchQuery').send_keys(ticker)
time.sleep(randint(15, 20))
driver.find_element_by_name('searchQuery').send_keys(Keys.ENTER)
time.sleep(randint(10, 45))
save_screenshot(driver, ticker + '.png', ticker)
def save_screenshot(driver, file_name, ticker):
height, width = scroll_down(driver)
driver.set_window_size(width, height)
img_binary = driver.get_screenshot_as_png()
img = Image.open(BytesIO(img_binary))
img.save('C:\\Users\\Johnn\\Desktop\\FA Sheet\\Sheets\\' + file_name)
# print(file_name)
print(ticker + " screendump saved ")
def scroll_down(driver):
total_width = driver.execute_script("return document.body.offsetWidth")
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
viewport_width = driver.execute_script("return document.body.clientWidth")
viewport_height = driver.execute_script("return window.innerHeight")
rectangles = []
i = 0
while i < total_height:
ii = 0
top_height = i + viewport_height
if top_height > total_height:
top_height = total_height
while ii < total_width:
top_width = ii + viewport_width
if top_width > total_width:
top_width = total_width
rectangles.append((ii, i, top_width, top_height))
ii = ii + viewport_width
i = i + viewport_height
previous = None
part = 0
for rectangle in rectangles:
if not previous is None:
driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
time.sleep(0.5)
# time.sleep(0.2)
if rectangle[1] + viewport_height > total_height:
offset = (rectangle[0], total_height - viewport_height)
else:
offset = (rectangle[0], rectangle[1])
previous = rectangle
return (total_height, total_width)