首页 > 解决方案 > Python Selenium - 无效的语法错误,尝试除外

问题描述

我一直在尝试编写一个 Python 脚本,该脚本导入并运行 Selenium 以模拟浏览器浏览网页,并且随着相关页面动态更新(ajax),某些元素将变得可用并被销毁,以便处理案例在哪里click()send_keys()不再存在的元素,我正在尝试实现try: except:来处理这个 - 代码示例如下:

                    SuspendedBanner = driver.find_elements_by_class_name('suspended-label ng-scope')
                    CheckInPlay = driver.find_elements_by_class_name('market-status-label')
                    if len(SuspendedBanner) == 0 and CheckInPlay(0).text == 'In-Play':
                        try:
                            driver.find_elements_by_class_name('rh-back-all-label ng-binding')(0).click()
                            PriceInputs = driver.find_elements_by_class_name('find_elements_by_class_name')
                            if len(PriceInputs) > 4:
                                for PriceInput in PriceInputs:
                                    PriceInput.send_keys('1.01')
                                    BackButtons = driver.find_elements_by_class_name('back mv-bet-button back-button back-selection-button')
                                    if len(BackButtons) == Len(PriceInputs):
                                        for Button in BackButtons:
                                            Prices.append(Button.find_elements_by_class_name('bet-button-price')[0].text
                                        #print(Prices)
                        except:
                            pass

标签: pythonpython-3.xselenium-webdrivererror-handlingtry-catch

解决方案


你少了一个括号。在此处显示的第一行中,有两个左括号,但只有一个右括号。

Prices.append(Button.find_elements_by_class_name('bet-button-price')[0].text
                                        #print(Prices)
                        except:
                            pass

通常情况下,语法错误消息将指向缺少括号(或],等)的那一行之后的一行。


推荐阅读