我有一個遍歷串列的搜索。對于搜索需要額外點擊彈出視窗的情況,我添加了一個嘗試。但是,嵌套嘗試的順序,除了會導致問題取決于彈出視窗的進入順序。使用當前代碼,應用程式被卡在 optionModalContent 彈出視窗上。此外,如果完成的搜索為 null 或空,則應用程式將超時。處理空/空搜索的最佳方法是什么?例如搜索 C06 或 0。
下面是我的代碼,其中包含應用程式的精簡版本。
from selenium import webdriver
import pandas as pd
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait, TimeoutException
import time
appended_data = []
classlist = ['C',
'C01',
'BETA BLOCKING AGENTS', #optionModalContent
'ANTIADRENERGIC AGENTS', #spellingModal
'C02',
'C06', #no search results timesout need to know how to handle this
'C07',
'Vitamins' #handled by optionModalContentTable
]
time.sleep(1)
with webdriver.Chrome('C:\Program Files\Chrome Driver\chromedriver.exe') as driver: ## uses the content manager by using with webdriver.Chrome, improves performance
for search in classlist:
page = f"https://mor.nlm.nih.gov/RxClass/search?query={search}"
driver = webdriver.Chrome('C:\Program Files\Chrome Driver\chromedriver.exe')
driver.get(page)
modal_wait = WebDriverWait(driver, 4)
try:
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID, 'synModalContent'))) ## handles last popup if the search isn't exact
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
try:
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID, 'optionModalContent'))) ## handles popup that requires you to select a line item
modal_el.find_element(By.CSS_SELECTOR, 'ul.uloption').click()
except:
try:
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID, 'spellingModal'))) ## handles popup to suggest spelling
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
except:
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID, 'optionModalContentTable'))) ##handles first popup window on search if there are multiple classes for vitamin
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
pass
except TimeoutException:
pass
table = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'tr.dbsearch')))
classid = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.table-responsive div.propText strong:nth-child(2)"))).text
classname = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.table-responsive div.propText strong:nth-child(1)"))).text
classtype = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.table-responsive div.propText strong:nth-child(3)"))).text
filename = classname[0:30] '.csv'
df = pd.read_html(driver.page_source)[1].iloc[:,:-1].assign(ClassID=classid, ClassName=classname, ClassType=classtype)
appended_data.append(df)
driver.quit()
appended_data = pd.concat(appended_data)
appended_data.to_csv('testcategorization.csv')
我知道我的問題將出現在我的嵌套嘗試中。我已經移動了一些東西并添加了通行證,但無論應用程式最終會卡在其中一個搜索上的順序如何。無論順序如何,處理所有彈出視窗的最佳方法是什么?
也許有更好的方法來嘗試和除外。此外,如果搜索沒有回傳任何結果,我不確定如何處理。
我正在尋找的最終結果是讓我的應用程式運行而不會卡在串列中的彈出視窗上,而不管它運行的順序如何。
uj5u.com熱心網友回復:
現在,這就是讓我前進的原因:
從當前 url 中獲取搜索鍵,并將代碼if elif else置于try塊中。您仍然可以通過try/except在每個if elif else陳述句中使用來改進它。
title = driver.current_url
print(title)
try:
if "BETA BLOCKING AGENTS" in title:
modal_el = modal_wait.until(EC.element_to_be_clickable(
(By.ID, 'optionModalContent'))) ## handles popup that requires you to select a line item
modal_el.find_element(By.CSS_SELECTOR, 'ul.uloption').click()
elif "ANTIADRENERGIC AGENTS" in title:
modal_el = modal_wait.until(
EC.element_to_be_clickable((By.ID, 'spellingModal'))) ## handles popup to suggest spelling
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
modal_wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='optionModalContent']//li"))).click()
elif "Vitamins" in title:
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID,
'optionModalContentTable'))) ##handles first popup window on search if there are multiple classes for vitamin
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
modal_wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='optionModalContent']//li"))).click()
else:
modal_el = modal_wait.until(
EC.element_to_be_clickable((By.ID, 'synModalContent'))) ## handles last popup if the search isn't exact
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
仍在檢查如何處理這種no classes found情況。如果我破解它,將在此處作為更新發布。將檢查我的空閑時間。我希望感謝您撰寫的代碼。除了打開一個額外的瀏覽器實體之外,它非常精簡,但我會說這是一個非常好的實體。我今天從你那里學到了一些東西。
更新:我嘗試了一些替代方案,它奏效了。請檢查以下代碼。它設法通過從搜索串列中搜索每個專案來獲得結果,同時處理C06.
我使用了很多用于除錯目的的列印陳述句。隨意洗掉它們。此外,我曾time.sleep在少數場合使用過警報處理。嘗試減少它,看看它是否有效。
for search in classlist:
page = f"https://mor.nlm.nih.gov/RxClass/search?query={search}"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(page)
time.sleep(5)
try:
WebDriverWait(driver, 3).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()
print("alert accepted")
except TimeoutException:
print("no alert")
title = driver.current_url
print(title)
modal_wait = WebDriverWait(driver, 4)
try:
if "BETA BLOCKING AGENTS" in title:
print("betablocker")
modal_el = modal_wait.until(EC.element_to_be_clickable(
(By.ID, 'optionModalContent'))) ## handles popup that requires you to select a line item
modal_el.find_element(By.CSS_SELECTOR, 'ul.uloption').click()
elif "ANTIADRENERGIC AGENTS" in title:
print("diuretic")
modal_el = modal_wait.until(
EC.element_to_be_clickable((By.ID, 'spellingModal'))) ## handles popup to suggest spelling
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
modal_wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='optionModalContent']//li"))).click()
elif "Vitamins" in title:
print("vitamin")
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID,
'optionModalContentTable'))) ##handles first popup window on search if there are multiple classes for vitamin
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
modal_wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='optionModalContent']//li"))).click()
else:
print("others")
modal_el = modal_wait.until(
EC.element_to_be_clickable((By.ID, 'synModalContent'))) ## handles last popup if the search isn't exact
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
except Exception as e:
print(f"No tables found in {search}")
continue
控制臺輸出(最終結果):
Process finished with exit code 0
更新 2:根據查詢創建者的評論處理嘗試/除外。
for search in classlist:
page = f"https://mor.nlm.nih.gov/RxClass/search?query={search}"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(page)
time.sleep(2)
try:
WebDriverWait(driver, 3).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()
print("alert accepted")
print(f"No tables found in {search}")
continue
except TimeoutException:
print("no alert")
modal_wait = WebDriverWait(driver, 4)
try:
modal_el = modal_wait.until(
EC.element_to_be_clickable((By.ID, 'synModalContent'))) ## handles last popup if the search isn't exact
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
except:
try:
modal_el = modal_wait.until(EC.element_to_be_clickable(
(By.ID, 'optionModalContent'))) ## handles popup that requires you to select a line item
modal_el.find_element(By.CSS_SELECTOR, 'ul.uloption').click()
except:
try:
modal_el = modal_wait.until(
EC.element_to_be_clickable((By.ID, 'spellingModal'))) ## handles popup to suggest spelling
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
except:
modal_el = modal_wait.until(EC.element_to_be_clickable((By.ID,
'optionModalContentTable'))) ##handles first popup window on search if there are multiple classes for vitamin
# modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
modal_el.find_element(By.CSS_SELECTOR, 'div.nameEntry').click()
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411734.html
標籤:
上一篇:當我將全域變數“Driver”宣告為None時,它??說Cannotfindreference'find_elements'in'None',也沒有建議
