从 Selenium 中的下拉元素中选择选项后页面未更新
P粉043470158
P粉043470158 2023-09-11 18:35:56
0
1
653

我遇到了这个问题,我试图在 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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板