繁体   English   中英

Selenium Python - 无论顺序如何,我如何处理弹出窗口上的异常并处理 null 或空搜索

[英]Selenium Python - How do I handle the exceptions on popups regardless of the order and handle a null or empty search

我有一个遍历列表的搜索。 对于搜索需要额外点击弹出窗口的情况,我添加了一个尝试。 但是,嵌套尝试的顺序,除了会导致问题取决于弹出窗口的进入顺序。使用当前代码,应用程序被卡在 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')  

       
          

我知道我的问题将出现在我的嵌套尝试中。 我已经移动了一些东西并添加了通行证,但无论应用程序最终会卡在其中一个搜索上的顺序如何。 无论顺序如何,处理所有弹出窗口的最佳方法是什么?

也许有一个更好的方法来 go 关于尝试和除外。 此外,如果搜索没有返回任何结果,我不确定如何处理。

我正在寻找的最终结果是让我的应用程序运行而不会卡在列表中的弹出窗口上,而不管它的运行顺序如何。

现在,这就是让我前进的原因:

从当前 url 中获取搜索键,并将代码置于try块下的if elif else中。 您仍然可以通过在每个if elif else语句中使用try/except来改进它。

title = driver.current_url
        print(title)
    try:
        if "BETA%20BLOCKING%20AGENTS" 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%20AGENTS" 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%20BLOCKING%20AGENTS" 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%20AGENTS" 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

控制台 Output(最终结果):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM