这是用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
请问怎么处理这样的报错,导出的代码哪里有问题
以下回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
根据报错信息 selenium.common.exceptions.NoSuchElementException
,可以推断出代码中的某个元素定位失败了。具体来说,是找不到 ID 为 acHeroBannerSlide-0-cta
的 div
元素下的 a
元素。
有些网站的页面元素可能会因为一些原因而动态改变,导致代码中的元素定位器失效。所以,为了避免这种情况,我们可以使用显式等待来等待页面元素加载完成后再进行操作。
在你的代码中,可以在找到 self.driver
元素之后,加入以下代码,使用显式等待等待元素加载完成:
wait = WebDriverWait(self.driver, 10) # 最长等待时间为10秒
element = wait.until(expected_conditions.presence_of_element_located((By.XPATH, "//div[@id='acHeroBannerSlide-0-cta']/a")))
这段代码将等待最长 10 秒,直到页面上的 //div[@id='acHeroBannerSlide-0-cta']/a
元素出现,然后将其赋值给 element
变量。如果 10 秒内元素没有出现,则会抛出 TimeoutException
异常。
将以上代码加入到你的代码中,应该可以解决你遇到的问题。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢