簡體   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