Selenium不会在for循环中打开新的url(Python和Chrome)

2024-09-29 17:13:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎无法让Selenium正确运行for循环。它第一次运行时没有问题,但当它启动第二个循环时,程序将停止运行,没有错误消息。当我用firefox浏览器尝试这个操作时,得到的结果是一样的。可能是因为我试图启动一个已经在运行的浏览器实例?任何帮助都将不胜感激。提前谢谢!在

def bookroom(self):
sessionrooms=["611", "618"] #the list being used by the for loop   
        driver = webdriver.Firefox() 
        #for loop trying each room
        for rooms in sessionrooms:
            room=roomoptions[rooms][0]
            sidenum=roomoptions[rooms][1]
            bookingurl="https://coparooms.newschool.edu/copa-scheduler/Web/reservation.php?rid="+room+"&sid="+sidenum+"&rd="+self.startdate
            driver.get(bookingurl)
            time.sleep(3)
            usernamefield = driver.find_element_by_id("email")
            usernamefield.send_keys(self.username)
            passwordfield = driver.find_element_by_id("password")
            passwordfield.send_keys(self.password)
            passwordfield.send_keys(Keys.RETURN)
            time.sleep(5)
            begin=Select(driver.find_element_by_name("beginPeriod"))
            print(self.starttime)
            begin.select_by_visible_text(convertarmy(self.starttime))
            end=Select(driver.find_element_by_name("endPeriod"))
            end.select_by_visible_text(convertarmy(self.endtime))
creates=driver.find_element_by_xpath("//button[contains(.,'Create')]")
            creates.click() #clicks the confirm button
            time.sleep(8)
            xpathtest=driver.find_element_by_id("result").text
            #if statement checks if creation was a success. If it is, exit the browser.
            if "success" in xpathtest or "Success" in xpathtest:
                print "Success!"
                driver.exit()
            else:
                print "Failure"
                time.sleep(2)
            #if creation was not success try the next room in sessionrooms

更新: 发现了问题,只是间距不均的问题。只有一些循环是“在循环中”。谢谢大家的帮助!在


Tags: theinselfidforbyiftime
1条回答
网友
1楼 · 发布于 2024-09-29 17:13:22
if "success" in xpathtest or "Success" in xpathtest:
    print "Success!"
    driver.exit()

在关闭驱动程序时,您需要在这里中断循环,但在循环的下一次迭代中使用它,而不启动新的驱动程序对象

相关问题 更多 >

    热门问题