简体   繁体   中英

Click Box Item Choices Selenium Python Not Retained

Script to automate collection of weather reports. Webpage allows click box selection of extra columns (see Add Columns dropdown box). I cannot seem to be able to retain my click box choices. (Running website manually does save my choices) I have tried to set my choices manually and saving /loading cookies. I have tried to click each click box using Selenium but choices are not retained. Otherwise, the script runs but only shows the basic number of weather data columns

'''

# -*- 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()

'''

You need to properly use WebDriverWait and correctly locate web elements.
Also, you should never mix implicitly_wait and WebDriverWait in the same code.
The following code opens the "Add Columns" dialog and celects "Dew point" checkbox.

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()

The result is

在此处输入图像描述

"""
Working version to suit my Selenium etc setup
"""


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\Mozilla Firefox\firefox.exe" # r"C:/location/to/Firefox/Binary/firefox.exe"

driver = webdriver.Firefox(options=options, executable_path = "C:\Windows\geckodriver.exe")

wait = WebDriverWait(driver, 20)

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

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

element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'uibox')))
element.click()

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

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='atemp']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='uv']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='humid']"))).click()
#wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='pres']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='vis']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='dew']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='precipC']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='layers']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='base']"))).click()

print("\n\nData Columns Reset: \n")

print("\n\nApparent Data Column Reset: \n")

placenames = ["London", "Manchester", "Birmingham"]

driver.maximize_window()

#for el in range(len(placenames)): # contined as original

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM