即使使用ui.WebDriverWait()chrome selenium python后也出现随机TimeoutException [英] Random TimeoutException even after using ui.WebDriverWait() chrome selenium python

查看:36
本文介绍了即使使用ui.WebDriverWait()chrome selenium python后也出现随机TimeoutException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码的随机超时异常不能确定解决这些问题的最佳方法是什么,并且这种超时并非始终发生,并且它确实会在某个时间或所有时间找到元素

Random timeout exception for the code below not sure whats the best approach to address these issues, and this timeout not happen all time, and also it does find elements sometime or all time

我们非常感谢您的评论和建议,显然,直到将元素加载到浏览器应用程序中或者元素在每次加载新页面时的间隔都不同的情况下,显式等待才会处理

we appreciate your comments and suggestions and apparently explicit wait is not handling until the elements gets loaded into the browser application or elements are getting different interval in every single time when new page gets loaded

"""

""

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)

推荐答案

如果您特别张贴引发超时异常的代码行,这将有助于更轻松地查找问题.

If you post the lines of code in particular that are throwing the timeout exceptions, this will help track down the issues easier.

我注意到您的大多数等待是等待 visibility_of_element_located .我建议尝试将其中一些更改为 element_to_be_clickable ,因为某些元素在完全呈现之前会出现在DOM上.

I noticed most of your waits are for visibility_of_element_located. I would recommend trying to change some of those to element_to_be_clickable instead, because some elements will appear on the DOM before they are fully rendered.

这篇关于即使使用ui.WebDriverWait()chrome selenium python后也出现随机TimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆