【问题标题】:Python Selenium - Invalid syntax error with try exceptPython Selenium - 无效的语法错误,尝试除外
【发布时间】:2019-12-09 07:54:07
【问题描述】:

我一直在尝试编写一个 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

【问题讨论】:

    标签: python python-3.x selenium-webdriver error-handling try-catch


    【解决方案1】:

    您缺少括号。此处显示的第一行有两个左括号,但只有一个右括号。

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

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

    【讨论】:

    • 天哪,我真是个白痴!感谢您指出了这一点!它有效!
    • 我马上就知道了,因为我自己已经做过很多次了。 :-)
    猜你喜欢
    • 2019-04-14
    • 2014-08-22
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多