繁体   English   中英

点击框项选择 Selenium Python 不保留

[英]Click Box Item Choices Selenium Python Not Retained

自动收集天气报告的脚本。 网页允许点击框选择额外的列(请参阅添加列下拉框)。 我似乎无法保留我的点击框选择。 (手动运行网站确实保存了我的选择)我尝试手动设置我的选择并保存/加载 cookies。我尝试使用 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()

'''

您需要正确使用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()

结果是

在此处输入图像描述

"""
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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