Selenium发送密钥导致点击功能中断

时间:2018-06-13 14:13:23

标签: python-3.x selenium geckodriver pyautogui

我正在使用Python,Selenium和Geckodriver为学校预订学习室建立预订机器人。预订房间的一步是搜索您想要的房间,然后点击该房间。当您点击房间时,您会看到一个下拉列表,其中显示您每天可以预订的时间(请参阅下面的图片)

Closed drop down

Open drop down

当我使用点击功能选择我的房间时,它会双击它,这将关闭下拉列表,然后打破剩下的代码。以下是我目前的代码(很好)

from selenium import webdriver
from pyautogui import press, typewrite, hotkey
import time

def function(user, password, room, startTime, endTime, date):
    #Troubleshooting
    print(user, password, room, startTime, endTime)
    #Get first website
    browser = webdriver.Firefox()
    browser.get("https://se.timeedit.net/web/liu/db1/wr_stud/")

    #Locate and click link to login
    logInLink = browser.find_element_by_xpath("//div[@id='logincontrol']/a[1]").click()
    #Enter credentials and log in
    usernameField = browser.find_element_by_xpath("//input[@id='userNameInput']").send_keys(user)
    passwordField = browser.find_element_by_xpath("//input[@id='passwordInput']").send_keys(password)
    logInButton = browser.find_element_by_xpath("//span[@id='submitButton']").click()

    #Enter booking page. Sleep to let the page load, search for room and wait for the room to show
    bookingPageLink = browser.find_element_by_xpath("//span[@class='utext']").click()
    roomField = browser.find_element_by_xpath("//input[@name='objectinputtext']").send_keys(room)
    time.sleep(2)

    #Select room, wait for dropdown to finish, choose date and enter time
    roomButton = browser.find_element_by_xpath("//*[text()='"+room+"']").click()
    time.sleep(2)
    dateButton = browser.find_element_by_xpath("//div[@data-dates='"+date+"']/div[@data-dates='"+date+"']").click()
    time.sleep(1)
    startTimeButton = browser.find_element_by_xpath("//td[@class='first']/select[1]").click()

    #Finish code!

if __name__ == "__main__":
    file = open("infoLiu.txt", 'r')
    info = file.readlines()
    file.close
    function(info[0].rstrip('\n'), info[1].rstrip('\n'), info[2].rstrip('\n'), info[3].rstrip('\n'), info[4].rstrip('\n'), info[5].rstrip('\n'))`

我在排除故障时注意到的一些奇怪的事情是,如果使用pyautogui中的typewrite()函数来输入"输入"在我想要的房间里,像这样。

    browser.find_element_by_xpath("//input[@name='objectinputtext']").click()
    typewrite(room)
    time.sleep(2)

    #Select room, wait for dropdown to finish, choose date and enter time
    roomButton = browser.find_element_by_xpath("//*[text()='"+room+"']").click()
    time.sleep(2)
    dateButton = browser.find_element_by_xpath("//div[@data-dates='"+date+"']/div[@data-dates='"+date+"']").click()
    time.sleep(1)
    startTimeButton = browser.find_element_by_xpath("//td[@class='first']/select[1]").click()
    #Finish code!

它会工作,而不是双击房间的暗示。

编辑:刚刚尝试在导致问题的点击后添加另一个点击工具,例如(睡眠(2)和睡眠(4)只是为了更容易排除故障):

    browser.find_element_by_xpath("//input[@name='objectinputtext']").send_keys(room)
    time.sleep(2)

    #Select room, wait for dropdown to finish, choose date and enter time
    roomButton = browser.find_element_by_xpath("//*[text()='"+room+"']").click()
    time.sleep(2)
    roomButton2 = browser.find_element_by_xpath("//*[text()='"+room+"']").click()
    time.sleep(4)
    dateButton = browser.find_element_by_xpath("//div[@data-dates='"+date+"']/div[@data-dates='"+date+"']").click()

第二次点击将像普通

一样执行

感谢任何帮助!!

0 个答案:

没有答案