2015-01-20 1 views
1

저는 비교적 간단한 질문이 있습니다. 나는 Selenium과 자바 스크립트라고 생각되는 팝업 창을 클릭하는 버튼을 얻으려고 노력하고있다. Firefox 용 Selenium 추가 기능을 실행했는데 코드가 나왔지만 작동하지 않습니다. 코드는 상당히 길고 모두 잘 작동하지만,이 선에 도달 할 때 :Selenium Python 자바 스크립트 팝업 버튼을 클릭해도 아무 것도 발생하지 않습니다.

버튼이 화면에 있음을

하지만,이 메시지와 함께 다음 오류를

driver.find_element_by_id("cancelExistingReservationButton").click() 

그냥 기다리고 그것은 절대로 클릭되지 않습니다. 나는 코드의 일부가 그것을 따르는 것을 알고 있지만, 나는 그 라인으로 아무 것도 할 수 없다.

Traceback (most recent call last): 
    File "C:/Users/Bradley/Desktop/testParkFare.py", line 34, in test_park_fare 
    driver.find_element_by_id("cancelExistingReservationButton").click() 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 65, in click 
    self._execute(Command.CLICK_ELEMENT) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 385, in _execute 
    return self._parent.execute(command, params) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 171, in execute 
    response = self.command_executor.execute(driver_command, params) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 349, in execute 
    return self._request(command_info[0], url, body=data) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 380, in _request 
    resp = self._conn.getresponse() 
    File "C:\Python27\lib\httplib.py", line 1074, in getresponse 
    response.begin() 
    File "C:\Python27\lib\httplib.py", line 415, in begin 
    version, status, reason = self._read_status() 
    File "C:\Python27\lib\httplib.py", line 379, in _read_status 
    raise BadStatusLine(line) 
    BadStatusLine: '' 

도움이된다면 정말 감사하겠습니다.

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import Select 
from selenium.webdriver.common.action_chains import ActionChains 
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.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import NoAlertPresentException 
import unittest, time, re 

class Test2(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.driver.maximize_window() 
     self.driver.implicitly_wait(60) 
     self.base_url = "https://disneyworld.disney.go.com" 
     self.verificationErrors = [] 
     self.accept_next_alert = True 

    def test_2(self): 
     driver = self.driver 
     driver.get(self.base_url + "/dining/grand-floridian-resort-and-spa/1900-park-fare/") 
     driver.find_element_by_xpath("(//button[@type='button'])[2]").click() 
     driver.find_element_by_link_text('27').click() 
     driver.find_element_by_css_selector("div.select-toggle.hoverable").click() 
     driver.find_element_by_id('diningAvailabilityForm-searchTime-0').click() 
     driver.find_element_by_css_selector("#partySize-wrapper > div.select-toggle.hoverable").click() 
     driver.find_element_by_id("partySize-3").click() 
     driver.find_element_by_css_selector("span.buttonText").click() 
     availabletimes = driver.find_element_by_xpath('//*[@id="timesContainer"]/div[2]').get_attribute("innerHTML") 

     availableTimesList = [] 

     startpoint = availabletimes.find("buttonText")+12 
     endpoint = availabletimes.find("span", startpoint)-2 

     allwantedtimes = ['8:45 AM', '8:40 AM', '8:50 AM', '8:55 AM', '8:35 AM', '8:30 AM', '8:25 AM', '8:20 AM', 
          '9:00 AM', '9:05 AM', '9:15 AM', '9:20 AM'] 

     positions = ["//div[@id='timesContainer']/div[2]/a/span/span/span", 
        "//div[@id='timesContainer']/div[2]/a[2]/span/span/span", 
        "//div[@id='timesContainer']/div[2]/a[3]/span/span/span"] 

     while startpoint > 500: 
      availableTimesList.append(availabletimes[startpoint: endpoint]) 
      startpoint = availabletimes.find("buttonText", endpoint)+12 
      endpoint = availabletimes.find("span", startpoint)-2 


     if len(availableTimesList) < 4: 
      for y in range(0, len(allwantedtimes)): 
       if allwantedtimes[y] in availableTimesList: 
        driver.find_element_by_xpath(positions[availableTimesList.index(allwantedtimes[y])]).click() 
        driver.find_element_by_id("loginPageUsername").clear() 
        driver.find_element_by_id("loginPageUsername").send_keys("ourlogin") 
        driver.find_element_by_id("loginPagePassword").clear() 
        driver.find_element_by_id("loginPagePassword").send_keys("ourpassword") 
        driver.find_element_by_id("loginPageSubmitButton").click() 
        print('We got here at least') 
        element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "cancelExistingReservationButton"))) 
        element.click() 
        print('But never here') 
        driver.find_element_by_id("cancelExistingReservationButton").click() 
        driver.find_element_by_id("continueWithExistingReservationButton").click() 
        driver.find_element_by_css_selector("#guestSelectBox0-wrapper > div.select-toggle.hoverable").click() 
        driver.find_element_by_css_selector("#guestSelectBox0-2 > span.rawOption").click() 
        driver.find_element_by_css_selector("#guestSelectBox1-wrapper > div.select-toggle.hoverable").click() 
        driver.find_element_by_css_selector("#guestSelectBox1-3 > span.rawOption").click() 
        driver.find_element_by_css_selector("#guestSelectBox2-wrapper > div.select-toggle.hoverable").click() 
        driver.find_element_by_css_selector("#guestSelectBox2-4 > span.rawOption").click() 
        driver.find_element_by_id("done").click() 
        driver.find_element_by_id("done").click() 
        driver.find_element_by_id("mobilePhone").clear() 
        driver.find_element_by_id("mobilePhone").send_keys("8659473353") 
        driver.find_element_by_id("nextButton").click() 
        driver.find_element_by_css_selector("span.rich-checkbox.unselected").click() 
        driver.find_element_by_id("nextButton").click() 
        break 







    def is_element_present(self, how, what): 
     try: self.driver.find_element(by=how, value=what) 
     except NoSuchElementException, e: return False 
     return True 

    def is_alert_present(self): 
     try: self.driver.switch_to_alert() 
     except NoAlertPresentException, e: return False 
     return True 

    def close_alert_and_get_its_text(self): 
     try: 
      alert = self.driver.switch_to_alert() 
      alert_text = alert.text 
      if self.accept_next_alert: 
       alert.accept() 
      else: 
       alert.dismiss() 
      return alert_text 
     finally: self.accept_next_alert = True 

    def tearDown(self): 
     self.driver.quit() 
     self.assertEqual([], self.verificationErrors) 

if __name__ == "__main__": 
    unittest.main() 
+0

어떻게됩니까 : 또한, 대신 presence_of_element_located 당신은 visibility_of_element_located 예상 조건을 사용해야합니까? 또한 웹 사이트에 대한 링크를 공유하고 지금까지 가지고있는 완전한 코드를 제공 할 수 있습니까? 감사. – alecxe

+0

단추를 수동으로 누르면 예상대로 작동합니다. 다음은 웹 사이트에 대한 링크입니다 : https : //disneyworld.disney.go.com/dining/grand-floridian-resort-and-spa/1900-park-fare/ – otictac1

+0

또한 원래 게시물에 전체 코드를 추가했습니다. – otictac1

답변

1

여기에서 로직을 변경해 보겠습니다. 먼저 로그인하여 페이지를 열고 예약하십시오. 수동 브라우저에서 버튼을 클릭하면

# log in 
driver.get(self.base_url + '/login/') 
driver.find_element_by_id('loginPageUsername').send_keys('ourlogin') 
driver.find_element_by_id('loginPagePassword').send_keys('ourpassword') 
driver.find_element_by_id('loginPageSubmitButton').click() 

driver.get(self.base_url + "/dining/grand-floridian-resort-and-spa/1900-park-fare/") 
driver.find_element_by_xpath("(//button[@type='button'])[2]").click() 
driver.find_element_by_link_text('27').click() 

... 

if len(availableTimesList) < 4: 
    for y in range(0, len(allwantedtimes)): 
     if allwantedtimes[y] in availableTimesList: 
      driver.find_element_by_xpath(positions[availableTimesList.index(allwantedtimes[y])]).click() 
      print('We got here at least') 
      element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "cancelExistingReservationButton"))) 
      element.click() 
      print('But never here') 
+0

잘 작동했습니다! 가시성 변경이 핵심이었던 것 같습니다. 너무 고맙습니다. – otictac1

1
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "cancelExistingReservationButton"))) 

element.click() 

당신은이 셀레늄에 의해 클릭 할 버튼이 장착되어 있는지 확인하고 준비하기 위해 시도 할 수 있습니다 : 여기

코드의 전체입니다.

+0

나는 그것을 시도했다. 그러나 불행하게도, 거의 같은 결과이었다. https://www.dropbox.com/s/hjf8b5n2p79kbow/Before.jpg?dl=0 웹 사이트가 어떻게 보이는지에 대한 링크는 다음과 같습니다. 위반 버튼이 화면에 표시 될 때의 모습은 다음과 같습니다. https : //www.dropbox.com/s/lfgvhlgphznsdf4/Popup%20After.jpg? dl = 0 – otictac1

관련 문제