gpt4 book ai didi

python - 即使使用 ui.WebDriverWait() chrome selenium python 后也会出现随机 TimeoutException

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:30 25 4
gpt4 key购买 nike

下面的代码出现随机超时异常,不确定解决这些问题的最佳方法是什么,并且这种超时并非总是发生,而且它有时或总是会找到元素

我们感谢您的意见和建议,显然显式等待不会处理,直到元素加载到浏览器应用程序中,或者元素在每次加载新页面时都获得不同的间隔

"""

“”“

import platform , logging
import os,re
from time import sleep
import selenium.webdriver.support.ui as ui
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

class Cloud(object):
"""
cloud class to get query and response
"""

def __init__(self, username='xxxxxx', password='xxxx'):

logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
self.logger = logging.getLogger(__name__)

self.url = "https://www.amazon.com"
self.username = username
self.password = password
self.timeout = 100
self.driver = None
self.get_chrome_driver()

def get_chrome_driver(self):
"""
get chrome driver
"""
if platform.system().lower() == 'windows':
if self.driver is None:
chrome_options = Options()
#chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--allow-insecure-localhost')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument("--log-level=3")
chrome_driver_path = os.path.join(str(os.environ['PYTHONPATH'].split(';')[0]),"bin","chromedriver","chromedriver.exe")
self.driver = WebDriver(executable_path=chrome_driver_path, chrome_options=chrome_options)
return self.driver

def login(self, username='xxxxxxx', password='xxxxx'):
"""
Login into amazon cloud
"""
self.logger.info("logging in amazon cloud username: %s and password: %s" %(self.username, re.sub(r".", "*", self.password)))
self.driver.get(self.url)
# wait for login username textbox

self.wait_visibility_element(By.XPATH, "//div[@id='nav-signin-tooltip']//span[@class='nav-action-inner'][contains(text(),'Sign in')]")
self.driver.find_element_by_xpath(" //div[@id='nav-signin-tooltip']//span[@class='nav-action-inner'][contains(text(),'Sign in')]").click()

self.wait_visibility_element(By.XPATH,"//label[@class='a-form-label']")

self.wait_visibility_element(By.XPATH,"//input[@id='ap_email']")
username_textbox = self.driver.find_element_by_xpath("//input[@id='ap_email']")
username_textbox.clear()
username_textbox.send_keys(self.username)
self.driver.find_element_by_xpath("//input[@id='continue']").click()
self.wait_visibility_element(By.XPATH,"//input[@id='ap_password']") #//label[@class='a-form-label']

password_textbox = self.driver.find_element_by_xpath("//input[@id='ap_password']")
password_textbox.clear()
password_textbox.send_keys(self.password)
# click on submit button
self.driver.find_element_by_xpath("//input[@id='signInSubmit']").click()


def wait_visibility_element(self, by_type, element_name):
"""
wait for visibility of element
:param by_type: Locate element using type of element
:param element_name: element name
"""
ui.WebDriverWait(self.driver, self.timeout).until(
EC.visibility_of_element_located((by_type, element_name)))

def get_audio_text(self, multi_turn_count=1):

self.login()
#Arrow in the Top Menu
self.wait_visibility_element(By.XPATH, "//a[@id='nav-link-accountList']//span[@class='nav-icon nav-arrow']")
ui.WebDriverWait(self.driver, self.timeout).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='nav-link-accountList']//span[@class='nav-icon nav-arrow']")))
self.driver.find_element_by_xpath("//a[@id='nav-link-accountList']//span[@class='nav-icon nav-arrow']").click()

#To avoid click event ambiguity
firstLevelMenu = self.driver.find_element_by_xpath("//span[contains(@class,'nav-line-2')][contains(text(),'Account & Lists')]")
action = ActionChains(self.driver)
action.move_to_element(firstLevelMenu).perform()

#sub menu select and click
self.wait_visibility_element(By.XPATH, "//span[contains(text(),'Your Content and Devices')]")
self.driver.find_element_by_xpath("//span[contains(text(),'Your Content and Devices')]").click()
#Alexa Privacy
self.wait_visibility_element(By.XPATH, "//div[@id='ng-app']//div[2]//div[1]//div[1]//div[1]//div[1]//div[1]//div[2]//div[6]//div[1]//div[1]")
self.driver.find_element_by_xpath("//div[@id='ng-app']//div[2]//div[1]//div[1]//div[1]//div[1]//div[1]//div[2]//div[6]//div[1]//div[1]").click()
self.wait_visibility_element(By.XPATH,'//div[@class="navAlexaOptionTitle_alexaNavHeader_myx ng-binding"][contains(text(),"Review Voice History")]')

ui.WebDriverWait(self.driver, self.timeout).until(
EC.element_to_be_clickable((By.XPATH, '//div[@class="navAlexaOptionTitle_alexaNavHeader_myx ng-binding"][contains(text(),"Review Voice History")]')))

ui.WebDriverWait(self.driver, self.timeout).until(EC.text_to_be_present_in_element((By.XPATH, '//span[@class="overviewHeadingString_myx ng-binding"]'), 'Alexa Privacy'))

self.driver.find_element_by_xpath('//div[@class="navAlexaOptionTitle_alexaNavHeader_myx ng-binding"][contains(text(),"Overview")]').click()
self.driver.find_element_by_xpath("//div[@class='navAlexaOptionTitle_alexaNavHeader_myx ng-binding'][contains(text(),'Review Voice History')]").click()

# Select the dropdown box
self.wait_visibility_element(By.XPATH,"//span[@id='timePickerDesktop']//span[@class='a-button-text a-declarative']")
ui.WebDriverWait(self.driver, self.timeout).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='timePickerDesktop']//span[@class='a-button-text a-declarative']")))
self.driver.find_element_by_xpath("//span[@id='timePickerDesktop']//span[@class='a-button-text a-declarative']").click()

#All history selection
self.wait_visibility_element(By.XPATH,"//a[@id='timePickerDesktop_4']")
ui.WebDriverWait(self.driver, self.timeout).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='timePickerDesktop_4']")))
self.driver.find_element_by_xpath("//a[@id='timePickerDesktop_4']").click()

# read first text format of the data
self.wait_visibility_element(By.XPATH,"//span[@id='mainInfo-0']//div[contains(@class,'summaryCss')]")
txt = self.driver.find_element_by_xpath("//span[@id='mainInfo-0']//div[contains(@class,'summaryCss')]").text
question_text = txt.encode("utf-8")[3:-3]

# Dropdown the rectangle menu
self.driver.find_element_by_xpath("//div[@id='arrowExpand-0']//i[@class='fa fa-angle-down caretAlignment']").click()

# read AVS Response
self.wait_visibility_element(By.XPATH,"//div[@id='activityItemsInner-0']//div[@class='ttsInfo']")
ui.WebDriverWait(self.driver, self.timeout).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='activityItemsInner-0']//div[@class='ttsInfo']")))
txt = self.driver.find_element_by_xpath("//div[@id='activityItemsInner-0']//div[@class='ttsInfo']").text
answer_text = txt.encode("utf-8")[3:-3]

self.sign_out_direct()
return question_text, answer_text

def sign_out(self):

#Sigout menu nevigation
self.driver.find_element_by_xpath("//i[@class='hm-icon nav-sprite']").click()
self.wait_visibility_element(By.XPATH,"//div[contains(text(),'SHOP BY CATEGORY')]")

#sign out
sign_out_element = self.driver.find_element_by_xpath("//li[44]//a[1]")
self.driver.execute_script("arguments[0].scrollIntoView();", sign_out_element)
#self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
ui.WebDriverWait(self.driver, self.timeout).until(EC.element_to_be_clickable((By.XPATH, "//li[44]//a[1]")))
self.driver.find_element_by_xpath("//li[44]//a[1]").click()

self.sign_out_direct()
#Close current tab
self.driver.close()

def sign_out_direct(self):

#Arrow in the Top Menu
self.wait_visibility_element(By.XPATH, "//a[@id='nav-link-accountList']//span[@class='nav-icon nav-arrow']")
ui.WebDriverWait(self.driver, self.timeout).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='nav-link-accountList']//span[@class='nav-icon nav-arrow']")))
self.driver.find_element_by_xpath("//a[@id='nav-link-accountList']//span[@class='nav-icon nav-arrow']").click()

#To avoid click event ambiguity
firstLevelMenu = self.driver.find_element_by_xpath("//span[contains(@class,'nav-line-2')][contains(text(),'Account & Lists')]")
action = ActionChains(self.driver)
action.move_to_element(firstLevelMenu).perform()

#sub menu select and click
self.wait_visibility_element(By.XPATH, " //span[contains(text(),'Sign Out')]")
self.driver.find_element_by_xpath("//span[contains(text(),'Sign Out')]").click()

#Close current tab
self.driver.close()

if __name__ == '__main__':

for loop in range(20):
PAGE = Cloud()
#PAGE.login()
OUTPUT = PAGE.get_audio_text()
print("\n\nQuestion:: %s"%str(list(OUTPUT)[0]).upper())
print("Answer:: %s"%str(list(OUTPUT)[1]).upper())
#PAGE.sign_out()
#PAGE.sign_out_direct()
sleep(2)

最佳答案

如果您特别发布引发超时异常的代码行,这将有助于更轻松地追踪问题。

我注意到您的大部分等待都是为了visibility_of_element_ located。我建议尝试将其中一些更改为 element_to_be_clickable,因为某些元素在完全渲染之前会出现在 DOM 上。

关于python - 即使使用 ui.WebDriverWait() chrome selenium python 后也会出现随机 TimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com