I'm using try
/except
to check for xpath
alternative sources for input into variables from a website.
It will have to do this many times, so I'm looking for a way to shorten the expression. Perhaps a context manager can be used somehow?
In this example, I am checking for two alternative xpath sources for the variables issuer
and 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