從 Selenium 中的下拉元素中選擇選項後頁面未更新
P粉043470158
P粉043470158 2023-09-11 18:35:56
0
1
637

我遇到了這個問題,我試圖在 Python 中自動化一個流程,該流程需要填寫網頁並從下拉式選單中選擇值。但是,我面臨一個問題,即即使選擇了下拉值,頁面也沒有更新。

這是選擇此特定元素的程式碼:

import os
import time
import pandas as pd
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options=Options()
#options.page_load_strategy='none'
options.add_experimental_option("detach", True)
options.add_argument('User_profile on mac')
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)

file=r'Example.xlsx'
webpage=r'url'

df_file=pd.read_excel(file)

browser.maximize_window()

for i,row in df_file.iterrows():
    time.sleep(3)
    browser.get(webpage)
    time.sleep(3)
   
    WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,'/htm
l/body/div[1]'))).click()
    cat2=browser.find_element(By.XPATH,'/html/body/span[1]')

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID,'s2id_autogen1'))).click() #looks for the first dropdown
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-result-label-21"]'))).click() #finds the correct option

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, 's2id_autogen3'))).click() #finds the second dropdown
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-results-4"]/li[11]'))).click() #clicks on load more
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="select2-result-label-34"]'))).click() #click on correct option
   
    problematic_dropdown=Select(browser.find_element(By.XPATH,'//*[@id="pageAside"]/div[2]/div/dep-gud-request-ors/dep-request-ors/es-view-form/form/dep-control-request-recipients/dep-control-request-recipient/div[2]/div/fieldset/dep-control-request-recipient-household/es-view-select2[1]/div/div[1]/div/select')) 

    browser.implicitly_wait(3)

    problematic_dropdown.select_by_value('1') #once this value is selected, the page does not load

我嘗試選擇不同的值,然後再次選擇所需的值;我嘗試過使用隱式和明確等待並使程式碼休眠 20 秒。不幸的是,沒有任何效果。當我手動登入網站並選擇值時,頁面會正常更新。

P粉043470158
P粉043470158

全部回覆(1)
P粉798010441

如果可以的話,我強烈建議使用更好的 xpath 選擇器,但是對於您要做什麼,只需直接單擊 option 元素即可,不需要先展開選擇框 p>

browser.find_element(By.XPATH,'//*[@id="pageAside"]/div[2]/div/dep-gud-request-ors/dep-request-ors/es-view-form/form/dep-control-request-recipients/dep-control-request-recipient/div[2]/div/fieldset/dep-control-request-recipient-household/es-view-select2[1]/div/div[1]/div/select/option[@value="1"]').click
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板