这是用selenium IDE测试后导出的python文件,倒是用python脚本运行时总是各种报错
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class Test():
def setup_method(self, method=None):
self.driver = webdriver.Firefox()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_(self):
self.driver.get("https://www.aircanada.com/cn/zh/aco/home.html")
self.driver.set_window_size(1936, 1056)
element = self.driver.find_element(By.XPATH, "//div[@id=\'acHeroBannerSlide-0-cta\']/a")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
element = self.driver.find_element(By.CSS_SELECTOR, "body")
actions = ActionChains(self.driver)
actions.move_to_element(element, 0, 0).perform()
self.driver.find_element(By.XPATH, "//input[@id=\'bkmgFlights_origin_trip_1\']").click()
self.driver.find_element(By.XPATH, "//input[@id=\'bkmgFlights_origin_trip_1\']").send_keys(Keys.DOWN)
element = self.driver.find_element(By.XPATH, "//div[@id=\'bkmg-tab-content-flight\']/ac-bkmg-flights-tab/form/fieldset/div/div/div/abc-typeahead/div/abc-input/abc-form-element-container/div/div/div/abc-affix/div/button")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
self.driver.find_element(By.XPATH, "//input[@id=\'bkmgFlights_destination_trip_1\']").click()
self.driver.find_element(By.XPATH, "//input[@id=\'bkmgFlights_destination_trip_1\']").send_keys("美国")
self.driver.find_element(By.XPATH, "//input[@id=\'bkmgFlights_origin_trip_1\']").send_keys(Keys.ENTER)
self.driver.find_element(By.XPATH, "//li[@id=\'bkmgFlights_destination_trip_1SearchResult0\']/abc-ripple/div").click()
self.driver.find_element(By.XPATH, "//input[@id=\'bkmgFlights_travelDates_1-formfield-1\']").click()
self.driver.execute_script("window.scrollTo(0,365)")
self.driver.find_element(By.XPATH, "//div[@id=\'bkmgFlights_travelDates_1-date-2023-04-14\']/div").click()
self.driver.find_element(By.XPATH, "//div[@id=\'bkmgFlights_travelDates_1-date-2023-05-31\']/div").click()
self.driver.find_element(By.XPATH, "//button[@id=\'bkmgFlights_travelDates_1_confirmDates\']/abc-ripple/div").click()
element = self.driver.find_element(By.XPATH, "//button[@id=\'bkmgFlights_travelDates_1_confirmDates\']/abc-ripple/div")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
self.driver.find_element(By.XPATH, "//button[@id=\'bkmgFlights_findButton\']/abc-ripple/div").click()
self.driver.find_element(By.XPATH, "//span[contains(.,\'关闭\')]").click()
if __name__ == '__main__':
mood = Test() # 创建对象
mood.setup_method()
mood.test_()
mood.teardown_method()
运行后报错:selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //div[@id='acHeroBannerSlide-0-cta']/a
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:180:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:392:5
element.find/</<@chrome://remote/content/marionette/element.sys.mjs:134:16
请问怎么处理这样的报错,导出的代码哪里有问题