单击框项目选项Selenium Python未保留

sgtfey8w  于 2023-01-09  发布在  Python
关注(0)|答案(1)|浏览(123)

脚本自动收集天气报告。网页允许点击框选择额外的列(见添加列下拉框)。我似乎无法保留我的点击框选择。(手动运行网站确实保存了我的选择)我曾尝试手动设置我的选择并保存/加载cookie。我曾尝试使用Selenium点击每个点击框,但选择不保留。否则,脚本运行,但仅显示天气数据列的基本数量
'''

# -*- coding: utf-8 -*-
"""
Windows 10 Desktop
Python 3.8
AUTOMATE COLLECTION OF IRISH WEATHER FORECASTS
https://www.xcweather.co.uk/forecast/gl50_4sh
"""

import time
# import os

# from PIL import Image

from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" 
driver = webdriver.Firefox(options=options, executable_path = "C:\Windows\geckodriver.exe")

driver.implicitly_wait(10)

driver.get("https://www.xcweather.co.uk/forecast/")

# save cookie preferences and exit acceptance button - click

wait = WebDriverWait(driver, 40) # explicit wait set 

element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'css-1uf3ch9')))
element.click()
print("\n\nThe First Window Opened: ", driver.title,"\n")

# select additional 9 data columns
# NOT WORK !!
# first reset

#element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reset')))

#element = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/table/tbody  /tr[2]\/td[3]/span[3]/div/div/form/table/tbody/tr[10]/td/a')))
#driver.execute_script("arguments[0].click();", element)
#element.click()
print("\n\nData Columns Reset: \n")

#element = wait.until(EC.element_to_be_clickable((By.NAME, 'atemp')))
#element = driver.find_element(('name', 'atemp'))
#element.click()
print("\n\nApparent Data Column Reset: \n")

placenames = ["Dublin", "Cork", "Limerick"]

driver.maximize_window()

for el in range(len(placenames)):
    
    time.sleep(3)
    # element = WebDriverWait(driver, 10).until(
    # EC.presence_of_element_located((By.ID, "myDynamicElement"))
    #input = driver.find_element(By.ID, "location-search-input")
   
    
    input = wait.until(EC.presence_of_element_located((By.ID, "loc_input")) ) 
    time.sleep(3)
    input.clear()
    time.sleep(3)
    input.send_keys(placenames[el])
    time.sleep(3)
    print("\nPLACE NAME ENTERED")
   
    button2 = wait.until(EC.element_to_be_clickable((By.ID, "searchbutton")))
    button2.click()
    time.sleep(3)
    
    print("\nThe Current Window Opened: ", driver.title,"\n")
    
    folder = "C:\\Users\\Robert\\Desktop"
    mypath = folder + "\\" + placenames[el] + "_XC" +'.png'
    
    # see https://reflect.run/articles/how-to-take-screenshot-inside-selenium-webdriver/
    
    # lambda function to find the value of X. We get the value by executing 
    # DOM JavaScript functions. The second line is to resize the window.
    S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
    # https://javascript.info/size-and-scroll-window
    print("\nS value: ",S)  
    
    driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
    time.sleep(3)
    driver.find_element_by_tag_name('body').screenshot(mypath)
    time.sleep(3)
    driver.save_screenshot(mypath)
    print("\nSCREENSHOT TAKEN")
    print("\nSCREENSHOT SAVED: ", mypath)
    
    time.sleep(3)
    
"""
CODE USED TO SAVE AND LOAD COOKIES
time.sleep(60)

driver.delete_all_cookies() # clean up
if os.path.exists('cookies.pkl'):
    cookies = pickle.load(open("cookies.pkl", "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie) # reaad in cookies to browser
    driver.refresh()
    time.sleep(5)
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb")) 
# save cookies to file

# You should add the above code just below the login code. 
# pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
# view pickle file in Word with utf8 encoding selected

"""
    
   
driver.quit()

'''

mf98qq94

mf98qq941#

您需要正确使用WebDriverWait并正确定位web元素。
此外,您不应该在同一代码中混用implicitly_waitWebDriverWait
下面的代码打开“添加列”对话框并取消选中“露点”复选框。

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

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://www.xcweather.co.uk/forecast/"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='Layout']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='dew']"))).click()

结果是

相关问题