我有一个相对简单的问题。我试图在一个弹出窗口上点击一个按钮,我相信它是与Selenium的javascript。我为Firefox运行了Selenium add,它给了我代码,但它没有用。代码相当长,一切正常,但是当它到达这一行时:
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 :(得分: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()
您可以尝试这样做以确保按钮已加载并准备好被Selenium点击。
答案 1 :(得分:1)
让我们在这里改变逻辑。首先 - 登录,然后打开页面并尝试进行预订。另外,您应该使用visibility_of_element_located
预期条件而不是presence_of_element_located
:
# 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')