xpath - Shorten expression for Python exceptions handling -


i'm using try/except check xpath alternative sources input variables website.

it have many times, i'm looking way shorten expression. perhaps context manager can used somehow?

in example, checking 2 alternative xpath sources variables issuer , name.

try:     xpath_issuer = ".//*[@id='dv_pre88f496c28ad6488895f1ffc383fae8bd_list_list']/div/div[3]/table/tbody/tr[2]/td[2]"     find_issuer = driver.find_element_by_xpath(xpath_issuer)     issuer = re.search(r"(.+)", find_issuer.text).group() except nosuchelementexception:     pass try:     xpath_issuer = ".//*[@id='dv_pre00e883469a264528b20fbbc31b0da4a2_list_list']/div/div[3]/table/tbody/tr[1]/td[2]/a"     find_issuer = driver.find_element_by_xpath(xpath_issuer)     issuer = re.search(r"(.+)", find_issuer.text).group() except nosuchelementexception:     pass try:     xpath_name = ".//*[@id='cols']/div[1]/div[1]/h1"     find_name = driver.find_element_by_xpath(xpath_name)     name = re.search(r"(.+)", find_name.text).group() except nosuchelementexception:     pass 

how this:

listxpath = [".//*[@id='dv_pre88f496c28ad6488895f1ffc383fae8bd_list_list']/div/div[3]/table/tbody/tr[2]/td[2]", ".//*[@id='dv_pre00e883469a264528b20fbbc31b0da4a2_list_list']/div/div[3]/table/tbody/tr[1]/td[2]/a", ".//*[@id='cols']/div[1]/div[1]/h1"] class work():     def __init__(self):         self.getissuer()      def getissuer(self):         in range(len(listxpath)):             xpath = listxpath[i]             try:                 find_issuer = driver.find_element_by_xpath(xpath_issuer)                 issuer = re.search(r"(.+)", find_issuer.text).group()             except:                 pass  # run clas work() 

you seems have 3 different options xpath_issuer loop through of those. set find_issuer. after issuer.

maybe can try let issuer return indicate if found. if pass issuer apparently worked, return statement.

for example:

listxpath = [".//*[@id='dv_pre88f496c28ad6488895f1ffc383fae8bd_list_list']/div/div[3]/table/tbody/tr[2]/td[2]", ".//*[@id='dv_pre00e883469a264528b20fbbc31b0da4a2_list_list']/div/div[3]/table/tbody/tr[1]/td[2]/a", ".//*[@id='cols']/div[1]/div[1]/h1"] class work():     def __init__(self):         x = self.getissuer()         print(x)      def getissuer(self):         in range(len(listxpath)):             xpath = listxpath[i]             try:                 find_issuer = driver.find_element_by_xpath(xpath_issuer)                 issuer = re.search(r"(.+)", find_issuer.text).group()                 return 'doneit'             except:                 pass  # run clas work() 

Comments

Post a Comment

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -