1

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
P A N
  • 5,642
  • 15
  • 52
  • 103
  • Define a function that takes an XPath expression and contains the try/except code, as shown at http://stackoverflow.com/a/12150013/423105. Have it return None when a NoSuchElementException occurs. Call the function three times, with three different XPath expressions... but stopping if the function returns a result other than None. – LarsH Jun 04 '15 at 19:44

1 Answers1

2

How about 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):
        for i 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 to have 3 different options for xpath_issuer so you loop through all of those. then you set find_issuer. After that you do something with issuer.

Maybe you can try to let issuer return something that will indicate if something is found. If you pass issuer apparently it worked, then do a 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):
        for i 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()
Tenzin
  • 2,415
  • 2
  • 23
  • 36
  • I see the 3rd one in your example is something else the `issuer`. I think my example will still give you an idea. You could also make 2 different definitions then. One for `issuer`, and one for `name`. – Tenzin Jun 04 '15 at 18:15
  • I tried your solution but I get `NameError: global name 'issuer' is not defined`. Any idea why? This happens when it tries to pass on `issuer` out of the loop. As long as it's in the loop, it can find `issuer`. – P A N Jun 04 '15 at 21:25
  • It seems like it successfully runs `try`, but then it also always runs `except` also, and that negates everything so nothing is passed on in `issuer`. Could that be right? – P A N Jun 04 '15 at 21:42
  • I don't realy have an understanding what `issuer` would be supposed to do? – Tenzin Jun 05 '15 at 06:29
  • `issuer` is the variable that I want to write to a csv later. It is extracted with `re.search` from `find_issuer`, which in turn comes from the xpath. – P A N Jun 05 '15 at 07:46