Rumah > Soal Jawab > teks badan
Saya menghadapi masalah ini, saya cuba mengautomasikan proses dalam Python yang memerlukan mengisi halaman web dan memilih nilai dari menu lungsur turun. Walau bagaimanapun, saya menghadapi isu di mana halaman tidak dikemas kini walaupun apabila nilai lungsur turun dipilih.
Ini ialah kod untuk memilih elemen khusus ini:
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
Saya telah cuba memilih nilai yang berbeza dan kemudian memilih nilai yang diingini sekali lagi; Malangnya, tiada apa yang berfungsi. Apabila saya log masuk ke tapak secara manual dan memilih nilai, halaman akan dikemas kini seperti biasa.
P粉7980104412023-09-12 12:45:17
Saya sangat mengesyorkan menggunakan pemilih xpath yang lebih baik jika anda boleh, tetapi untuk apa yang anda mahu lakukan, klik terus pada elemen option
, tidak perlu mengembangkan kotak pilihan terlebih dahulu 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